class dependence

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jon Slaughter

    class dependence

    I have created a few classes which depend on other classes but I'm unsure
    how to go about including the classes so they are visible to each other.

    Can I just require_once at some point before use and make sure the order is
    correct? or can I insert the require_once in the class itself(which then I'm
    worried about forcing the location)? How does one normally go about this?

    Thanks,
    Jon


  • ZeldorBlat

    #2
    Re: class dependence

    On May 29, 3:54 pm, "Jon Slaughter" <Jon_Slaugh...@ Hotmail.comwrot e:
    I have created a few classes which depend on other classes but I'm unsure
    how to go about including the classes so they are visible to each other.
    >
    Can I just require_once at some point before use and make sure the order is
    correct? or can I insert the require_once in the class itself(which then I'm
    worried about forcing the location)? How does one normally go about this?
    >
    Thanks,
    Jon
    Use __autoload() -- it'll make your life a lot easier:

    <http://www.php.net/autoload>

    Here's what mine looks like (my files are named after my classes, so a
    class called Foo lives in a file called Foo.php):

    if(!function_ex ists('__autoloa d')) {
    function __autoload($cla ss_name) {
    require_once($c lass_name . '.php');
    }
    }

    ini_set('unseri alize_callback_ func', '__autoload');


    Put all that in a file (call it autoload.php or something), then just
    require_once that file on all your pages. No need to include each
    class separately -- only things that are needed will be loaded (and in
    the right order, too). Problem solved!

    Comment

    • Jon Slaughter

      #3
      Re: class dependence


      "ZeldorBlat " <zeldorblat@gma il.comwrote in message
      news:1180471969 .899593.68390@k 79g2000hse.goog legroups.com...
      On May 29, 3:54 pm, "Jon Slaughter" <Jon_Slaugh...@ Hotmail.comwrot e:
      >I have created a few classes which depend on other classes but I'm unsure
      >how to go about including the classes so they are visible to each other.
      >>
      >Can I just require_once at some point before use and make sure the order
      >is
      >correct? or can I insert the require_once in the class itself(which then
      >I'm
      >worried about forcing the location)? How does one normally go about
      >this?
      >>
      >Thanks,
      >Jon
      >
      Use __autoload() -- it'll make your life a lot easier:
      >
      <http://www.php.net/autoload>
      >
      Here's what mine looks like (my files are named after my classes, so a
      class called Foo lives in a file called Foo.php):
      >
      if(!function_ex ists('__autoloa d')) {
      function __autoload($cla ss_name) {
      require_once($c lass_name . '.php');
      }
      }
      >
      ini_set('unseri alize_callback_ func', '__autoload');
      >
      >
      Put all that in a file (call it autoload.php or something), then just
      require_once that file on all your pages. No need to include each
      class separately -- only things that are needed will be loaded (and in
      the right order, too). Problem solved!
      >
      Well, that is essentially what I'm doing. Since I have a central php file
      that handles everything I can just require it in that and it will act the
      same... the problem is that the order has to be right and so the dependence
      could get screwed up ;/ Was trying to avoid it if possible.

      I might try that though,

      Thanks,
      Jon


      Comment

      • ZeldorBlat

        #4
        Re: class dependence

        On May 29, 5:23 pm, "Jon Slaughter" <Jon_Slaugh...@ Hotmail.comwrot e:
        "ZeldorBlat " <zeldorb...@gma il.comwrote in message
        >
        news:1180471969 .899593.68390@k 79g2000hse.goog legroups.com...
        >
        >
        >
        On May 29, 3:54 pm, "Jon Slaughter" <Jon_Slaugh...@ Hotmail.comwrot e:
        I have created a few classes which depend on other classes but I'm unsure
        how to go about including the classes so they are visible to each other.
        >
        Can I just require_once at some point before use and make sure the order
        is
        correct? or can I insert the require_once in the class itself(which then
        I'm
        worried about forcing the location)? How does one normally go about
        this?
        >
        Thanks,
        Jon
        >
        Use __autoload() -- it'll make your life a lot easier:
        >
        <http://www.php.net/autoload>
        >
        Here's what mine looks like (my files are named after my classes, so a
        class called Foo lives in a file called Foo.php):
        >
        if(!function_ex ists('__autoloa d')) {
        function __autoload($cla ss_name) {
        require_once($c lass_name . '.php');
        }
        }
        >
        ini_set('unseri alize_callback_ func', '__autoload');
        >
        Put all that in a file (call it autoload.php or something), then just
        require_once that file on all your pages. No need to include each
        class separately -- only things that are needed will be loaded (and in
        the right order, too). Problem solved!
        >
        Well, that is essentially what I'm doing. Since I have a central php file
        that handles everything I can just require it in that and it will act the
        same... the problem is that the order has to be right and so the dependence
        could get screwed up ;/ Was trying to avoid it if possible.
        >
        I might try that though,
        >
        Thanks,
        Jon
        When you use __autoload you don't need to worry about the order in
        which you include your classes -- it all happens automatically. As
        long as you include it before you use any classes it shouldn't be a
        problem.

        Comment

        Working...