Class question.

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

Hi group,

If i need to create a object with code like "$cConfig = new config();" do I
have to include the file where the class is declared?
Something like:
     require "$dir_site/lib/config.obj.php";

Thanks in advance,

Nuno Paquete

Re: Class question.

yes, and if you're not sure where or if it's already included, you can


and if the file is already included, it will then ignore subsequent

Re: Class question.

In PHP 5 you can also define __autoload:

So, if you name all your class definition files /lib/someclass.obj.php,
you would define __autoload as such:

function __autoload($class_name) {
    require_once "/lib/" . $class_name . ".obj.php";

Put that function in an include (autoloader.php or something) and then
require_once autoloader.php at the top of your pages.  If you try to
instantiate a class and PHP doesn't know about it, it executes the
autoloader and you class definition file will be included
automatically.  This is especially useful if you have a lot of classes
(each in their own file) and you don't want a whole bunch of includes
at the top of your pages.

Site Timeline