Class question.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nuno Paquete

    Class question.

    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


  • unruly

    #2
    Re: Class question.

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

    requrie_once('p ath/to/file.php');

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

    Comment

    • ZeldorBlat

      #3
      Re: Class question.

      In PHP 5 you can also define __autoload:



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

      function __autoload($cla ss_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.

      Comment

      Working...