Should PHP scripts have multiple classes in the same file?
I not sure where I heard this, but I heard/read a little while ago that:
"Classes are not recommended in small php applications/scripts, as they need to be called on each request."
开发者_如何学JAVABut isn't this the same as calling functions on each request?
Also, how do you include several classes into a application/script, as I have seen that people have a simple file that include all the classes.
Isn't there a better way to include all the classes into a application/script?
Cheers!
As you can not even give source of the comment, I suggest to drop it completely because it looks like that it is totally indifferent and therefore a waste of time to deal with.
This should lighten up to answer your actual questions as well:
But isn't this the same as calling functions on each request?
Not the same but quite comparable, right.
Also, how do you include several classes into a application/script, as I have seen that people have a simple file that include all the classes. Isn't there a better way to include all the classes into a application/script?
Define better? What's wrong with that approach? Generally spoken, there are many ways how this can be acomplished, however it relates to how the classes are organized in files and then how the files are organized in the file-system.
Normally you include the file's classes on need, either manually or by using an autoloader. And that's pretty much is it. See the links on the right side you will find tons of information.
As yes123 says, that quote doesn't make sense. It is up to you as the developer to decide how your project should be coded and whether or not it should use classes.
There are several schools of thought on how to best include classes. Personally, I prefer to have a seperate file for each class, the file having the same name as the class, so a class called mySuperClass would be in a file called mySuperClass.php and included with require_once 'mySuperClass.php';
.
Other people like to put their classes into files containing several related classes. I don't think it matters so long as you decide what you want to do and you're consistant about it.
You may want to look at autoloading too and class/file naming conventions such as PEAR. I use Zend Framework for most of my projects, so tend to stick with autoloading and the PEAR naming convention. http://framework.zend.com/manual/en/coding-standard.naming-conventions.html other people will have their own preferences for this.
PHP 5.3 has also introduced namespacing which gives another approach, there is an introduction to it here:- http://www.sitepoint.com/php-53-namespaces-basics/ I haven't really taken to that as I quite like my current system, but it may suit you.
you can use __autoload. It will include class when it need
function __autoload($class_name) {
include 'classes/'.$class_name . '.php';
}
精彩评论