My goal this year is to try and wrap my head around PHP OOP. One thing I’ve been noticing is that you can apparently access public static methods from another Class from within a different Class without including the first Class file. Is this correct? I’m so used to including a file first before you can access any functions or variables from it, but with Classes, one can apparently use static methods without including them first. Do the Class files need to be in the same directory for this to work?
So many questions…such limited brain capacity…:sick:
No, this is not exactly correct.
Yes, you can access public static method of one class from another class but you still need to include that class using any of the include techniques: require or include or rely of autoloader function to find and include that class for you.
File containing that class has to be included one way or the other.
Remember in php you can have multiple classes per file, it’s up to you. Usually it’s a good design practice to write one class per file, but this is not Java, and you can even write all your project’s classes in one very big file.
Thanks for that clear explanation. The book I’m reading creates several files that use static methods from other Classes, but I looked ahead a few pages and saw that they are all included in an index.php file using the MVC pattern.
You don’t need to instantiate an object, but as mentioned the file must be included.
Does this book include ALL the classes used in the app in index.php automatically?
It includes them using “require”, but does not auto-load them, if that’s what you mean.