SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Running every constructor on extends

    PHP Code:
    class Login
    {
        function 
    Login()
        {
            
    // Do stuff here
        
    }

    PHP Code:
    class System extends Login
    {
        function 
    System()
        {
            
    $this->Login() // <- What I do now
            // Do stuff here
        
    }

    PHP Code:
    class SomePage extends System
    {
        function 
    SomePage()
        {
            
    $this->System() // <- What I do now
            // Do stuff here
        
    }

    So my basic question is, is there a method I can use to get it to run the Login() constructor and System() constructor automaticall when calling SomePage()? Or do I have to stick with my current method?
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  2. #2
    SitePoint Zealot Sork's Avatar
    Join Date
    Jul 2002
    Location
    Portugal
    Posts
    143
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    class SomePage extends System
    {
        function 
    SomePage()
        {
           
    // Since this works
           
    System::System();
           
    // This should work too
           
    Login::Login();
        }


  3. #3
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh, my current method works fine, but I was wondering if there was a more 'automatic' method available in PHP.
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  4. #4
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Everything I've read on the topic indicates you have to do it manually.

    On a side note, using static::method instead of just using $this->method seems like asking for it. If you were to change the inheritance of SomePage to (for example) SystemY, you would still be using System::System() instead of SystemY::System() which may override the former. Hope that made sense (and was correct!).
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  5. #5
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it did make sense, and also, I need to be calling it using $this->method, because it sets object variables and such.
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  6. #6
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is all well and good now, but what about when PHP5 comes along, will I have to stick to using ClassName()? Or is there a way to do it with __construct()...
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  7. #7
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good question... I don't know! Haven't looked into PHP5 much yet.

    I've read that PHP 5 will support both styles of constructors which leaves me confused about what they would do if they had slightly different features and one began mixing the two in extended classes.

    Anyway, if you don't get a response here you might try sitepoint member nucleuz, he has a PHP5 site (worth examining).
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  8. #8
    public static void brain Gybbyl's Avatar
    Join Date
    Jun 2002
    Location
    Montana, USA
    Posts
    647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can just do parent::method() instead of actually typing out the parent class name... Although I'm not sure of the exact syntax for calling the constructor of the parent class; i'm sure there's a way, let me go find out.

    If you are using PHP5, then you could just use parent::__construct().

    What you could also do, if you want to do a little hardcoding is:

    Define a method called "constructor()" in every class that you plan on using in your hierarchy. Now, put your constructor code in this method and then just call this method from the constructor. That way, you can also call parent::constructor() in that same method, and run all of the constructors all the way up the hierarchy. Not sure if it works, but it sounds good so far in theory. =]

    you can also use "get_parent_class()" to return the name of the class, and then instantiate it in the method where you want to call the parent constructor.
    Last edited by Gybbyl; Mar 31, 2003 at 21:05.
    Ryan

  9. #9
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    parent::__construct(); works well, many thanks for that
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  10. #10
    public static void brain Gybbyl's Avatar
    Join Date
    Jun 2002
    Location
    Montana, USA
    Posts
    647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, no problem

    What version is it working with? 5? or does it actually work with 4??
    Ryan

  11. #11
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it's working with 5, although I'd assume it would work with PHP4 anyways, if you call your function __construct() and call it, although I need to add a couple extra lines to get it to be PHP4 compatible.
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •