lnthai2002 Posted July 5, 2006 Share Posted July 5, 2006 Hi guys,I am new to php programming. I read some php book and know that i can write a class similar to java in php and save it as an inc file to instantiate latter, but i can use the require directive to load a file to the body of the php file. So the question is that which way is better in practice in order to reuse code (modularize code)?Hope to hear some suggestion.Thanks in advanceThai Quote Link to comment Share on other sites More sharing options...
trq Posted July 5, 2006 Share Posted July 5, 2006 Your talking about two completely different things. You create a class, put it in a file, then call require() to include that file (and the class it contains) in another file. Quote Link to comment Share on other sites More sharing options...
lnthai2002 Posted July 13, 2006 Author Share Posted July 13, 2006 Hi, Thanks for your explaination but i still dont understand completly.I can write some function (without class definition) and put in a file then call require() to include that file in other file to use it.With class definition, i HAVE to do the same thing except that i HAVE to instantiate the class which has been included before i can use it! So what 's the point of using object oriented programming in web programming? Extra work, the same functionality and same data need to be sent over network(assumming apache server is on a different machine than the php files, thus, in order to process a completed php file, apache must fetch the class file and the main php file which uses the class!)Hope anyone can clearify my concernThanks in advance Quote Link to comment Share on other sites More sharing options...
hvle Posted July 13, 2006 Share Posted July 13, 2006 you write bunch of functions and save it as functions.phpyou write one class and save as class1.phpnow in a script, you'll require:require_once('functions.php');require_once('class1.php');// you can call a function here// you can instantiate a class here:$myclass = new class1();class1.php is only definition. If you like instatiate the same class in side the class1.php, it's your choice, but most people instantiate a class object inside the script unless they have a good reason to do so. Quote Link to comment Share on other sites More sharing options...
gewthen Posted July 13, 2006 Share Posted July 13, 2006 [quote author=lnthai2002 link=topic=99477.msg396088#msg396088 date=1152757340]With class definition, i HAVE to do the same thing except that i HAVE to instantiate the class which has been included before i can use it! [/quote]Not true. I assume that you are used to putting one class per file in the java style. You can do the same thing with php, but it require you use the [url=http://www.php.net/manual/en/language.oop5.autoload.php]autoloader [/url]functionality. The autoloading allows you forget to include class files explictly. Once the class is included you do not have to instantiate the class to use it. You can call static methods on it. Example:[code]class DataSack{ public static function max($num1, $num2){ if($num1<$num2) return $num2; return $num1; }}[/code]Now with the autoloader you can do:[code]echo DataSack->max(10, 12);[/code]For example sake, it'll produce:[code]12[/code]I hope this addressing your problem of not being able to use a class without instantiating the class. I assumed you are working with PHP 5. If you are stuck with PHP 4, I suggest forgetting about using object and use procedural programing. Quote Link to comment Share on other sites More sharing options...
ShogunWarrior Posted January 3, 2007 Share Posted January 3, 2007 You could just execute the function statically in both PHP 4/5 though: [code] DataStack::max(10,12);[/code] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.