I am not too familiar with PHP Classes nor Object-Oriented Programming.
I have a site that was coded in PHP and is written entirely using Classes.
I want to add a new section to the site based on a script I already have that is NOT a Class.
The main file of this script has several functions, and it also includes external functions, none of which are in a 开发者_开发知识库Class.
What would I have to do to get the script to work with the current OOP site? In other words, how can I convert the PHP script into a Class such that all the calls to functions and includes work within the new Class without any errors?
Thank you.
Rewrite all non-oop class.
I know it's propably complicated, but the only good solution.
For weak solutions (not recommended, they are crapy)
you can check request at start of your php file and include script #1 or script#2
you can set non-oop script somewher to only output data and write some little module in second script to get contents of first by file_get_contents, curl, ajax request or sth
Don't think about 'class'-'non class' difference, it's not the point, the point is architecture/frameworks of both scripts, way to create modules/functionality in each. I mean that, class-written code CAN work with non-class written rest.
There is no way of getting a satisfactory answer because it all depends on the case at hand. For instance, output could be handled by smarty. In that case, your script doesn't need to be OOP, you just have to use the object for your output.
If you really want it to be OOP, the first thing you should do is re-factor your code so it only uses functions. The stuff that is always executed and cannot be put into a function goes into the constructor. You wrap that all into a class and there you go, a new class.
But before you do that, you should analyze the code and ask yourself if you really need the code to be OOP. Do you have to micro-manage dozens of variables and arrays just so you have the needed values always at hand? Do you have a lot of functions that change/manage/output variables that go together? In that case you should use OOP. In any other case, you're better off using regular functions.
You want to add another page? You have to find how you can add controllers to the app, this is entirely framework dependent.
Regarding Class vs non-Class script: this is not a problem, but you probably have to write a small wrapper class for your existing code. On the other hand if your code is relatively small, then of course it makes sense to refractor it.
精彩评论