Jump to content

access class member


lenerd3000

Recommended Posts

is there's a way how can i access parent class member in another class?

 

class test

{

    function t();{}

}

 

class test2 extends test

{

    function t2();{}

}

 

class test3 extends test

{

    function t3();

    {

          // can i access t2 from here which is also a member of test class?

    }

}

 

 

Link to comment
Share on other sites

you can only access it if t3 extends from t2.

 

e.g.

class t3 extends t2 {
  function t3(){
    $this->t2();
  }
}

 

What concerns me however is that you appear to be using php4 syntax. Convert to 5 before it's too late, otherwise you'll get left in the dark php ages...

Link to comment
Share on other sites

It is PHP 5 syntax. I think aschk is referring to that you are not explicitly declaring the methods as public. However, if you omit a visibility declaration for a method, then it will always be public implicitly. It's standard practice to always include it though.

Link to comment
Share on other sites

I think my sight was funny that day. For some reason or another I thought you were using the old fashioned constructor methods (i.e. for class test, you have a method called test). However, i realise you're not ... my bad.

 

Anywho, the method (t2) that you were interested in using from test3 is a sibling method, and not a parent method. You can't utilise sibling methods as this breaks the fundamental rule of inheritance ;)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.