Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jan 2010
    Posts
    101
    Thanks
    17
    Thanked 5 Times in 5 Posts

    Access One Child Class from Another

    I have a main class, and multiple child classes that extend the main class. What I want to do is access a method of one child class in another child class. Here's an example:

    PHP Code:
    class Hello {
         public function 
    __construct(){
              echo(
    "Hello. ");
         }
    }
    class 
    Message1 extends Hello {
         public function 
    __construct(){
              
    $message "I am 1.";
         }
    }
    class 
    Message2 extends Hello {
         public function 
    __construct(){
              echo(
    parent::Message1); // This is what I am trying to figure out how to do. I need to access a variable declared by a sibling class.
         
    }
    }
    $init = new Hello;
    $message1 = new Message1;
    $message = new Message2
    Any clarification on how to do this would be fantastic.

  • #2
    Regular Coder
    Join Date
    Jan 2010
    Posts
    101
    Thanks
    17
    Thanked 5 Times in 5 Posts
    Found out I can initiate the first Message class, then pass that object to the next Message class, which will probably work for what I need it for. If anyone has a better solution I'd still like to hear it!

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    No, that doesn't make any sense. Just as a parent is unaware of a child, a child is unaware of any siblings. The only way to do this is to either instantiate a new child or to pass an object to another object. The plus side is since they all inherit from the same class, any visible scope members or methods are available on any passed object as well, so you can access an inherited protected method from Hello in Message1 and Message2 from either of the class instances.

    There is no reliable way to do this through the use of inheritance. While a parent can access a child through the use of dynamic binding on a method call, the runtime class is what determines which datatype is making the call, so you cannot tell which it was. get_class can tell you which runtime it is, but not about any other classes which could be done with some heavy reflection of all declared classes.
    Depending on what you are doing depends if there is an easier way. A log interceptor for example would use a decorator, which allows you to wrap Logger into Logger over and over again, and when called it will determine which logger to call based on the provided information (ie: notice logger, warning logger, critical logger for examples).
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #4
    Regular Coder
    Join Date
    Jan 2010
    Posts
    101
    Thanks
    17
    Thanked 5 Times in 5 Posts
    Alright thanks, looks like I'll be passing the object then


  •  

    Posting Permissions

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