php: property inheritance on nested class structure











up vote
1
down vote

favorite












It's not clear to me how class inheritance is implemented in php 5.4.7 (almost old! I know!).
Consider this example:



Class ClassA {
public $property = array();
function __construct() {
$this->property = "ClassA.construct";
}
public function SetA() {
$this->property = "ClassA.Set";
}
}

Class ClassB extends ClassA {
function __construct() {
$this->property = "ClassB.construct";
}
function SetB() {
$this->property = "ClassB.Set";
}
}


If I call in sequence



$classA = new ClassA();
$classA->SetA();
$classB = new ClassB();
$classB->SetB();
print_r($classB->property);


My expected behavior is to have...



Array
(
[0] => ClassA.construct
[1] => ClassA.Set
[2] => ClassB.construct
[3] => ClassB.Set
)


...but I obtain instead...



Array
(
[0] => ClassB.construct
[1] => ClassB.Set
)


So, what's wrong on my side?
How can I add element from a Child to an array defined on Parent object?










share|improve this question


























    up vote
    1
    down vote

    favorite












    It's not clear to me how class inheritance is implemented in php 5.4.7 (almost old! I know!).
    Consider this example:



    Class ClassA {
    public $property = array();
    function __construct() {
    $this->property = "ClassA.construct";
    }
    public function SetA() {
    $this->property = "ClassA.Set";
    }
    }

    Class ClassB extends ClassA {
    function __construct() {
    $this->property = "ClassB.construct";
    }
    function SetB() {
    $this->property = "ClassB.Set";
    }
    }


    If I call in sequence



    $classA = new ClassA();
    $classA->SetA();
    $classB = new ClassB();
    $classB->SetB();
    print_r($classB->property);


    My expected behavior is to have...



    Array
    (
    [0] => ClassA.construct
    [1] => ClassA.Set
    [2] => ClassB.construct
    [3] => ClassB.Set
    )


    ...but I obtain instead...



    Array
    (
    [0] => ClassB.construct
    [1] => ClassB.Set
    )


    So, what's wrong on my side?
    How can I add element from a Child to an array defined on Parent object?










    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      It's not clear to me how class inheritance is implemented in php 5.4.7 (almost old! I know!).
      Consider this example:



      Class ClassA {
      public $property = array();
      function __construct() {
      $this->property = "ClassA.construct";
      }
      public function SetA() {
      $this->property = "ClassA.Set";
      }
      }

      Class ClassB extends ClassA {
      function __construct() {
      $this->property = "ClassB.construct";
      }
      function SetB() {
      $this->property = "ClassB.Set";
      }
      }


      If I call in sequence



      $classA = new ClassA();
      $classA->SetA();
      $classB = new ClassB();
      $classB->SetB();
      print_r($classB->property);


      My expected behavior is to have...



      Array
      (
      [0] => ClassA.construct
      [1] => ClassA.Set
      [2] => ClassB.construct
      [3] => ClassB.Set
      )


      ...but I obtain instead...



      Array
      (
      [0] => ClassB.construct
      [1] => ClassB.Set
      )


      So, what's wrong on my side?
      How can I add element from a Child to an array defined on Parent object?










      share|improve this question













      It's not clear to me how class inheritance is implemented in php 5.4.7 (almost old! I know!).
      Consider this example:



      Class ClassA {
      public $property = array();
      function __construct() {
      $this->property = "ClassA.construct";
      }
      public function SetA() {
      $this->property = "ClassA.Set";
      }
      }

      Class ClassB extends ClassA {
      function __construct() {
      $this->property = "ClassB.construct";
      }
      function SetB() {
      $this->property = "ClassB.Set";
      }
      }


      If I call in sequence



      $classA = new ClassA();
      $classA->SetA();
      $classB = new ClassB();
      $classB->SetB();
      print_r($classB->property);


      My expected behavior is to have...



      Array
      (
      [0] => ClassA.construct
      [1] => ClassA.Set
      [2] => ClassB.construct
      [3] => ClassB.Set
      )


      ...but I obtain instead...



      Array
      (
      [0] => ClassB.construct
      [1] => ClassB.Set
      )


      So, what's wrong on my side?
      How can I add element from a Child to an array defined on Parent object?







      php oop inheritance






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 at 8:19









      Stefano Radaelli

      480627




      480627
























          4 Answers
          4






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          In PHP, parent constructors aren't called automatically, to get your behaviour you need to do the following:



          Class ClassB extends ClassA {
          function __construct() {
          parent::__construct();
          $this->property = "ClassB.construct";
          }
          function SetB() {
          $this->property = "ClassB.Set";
          }
          }


          And, at most, you'll get this



          Array
          (
          [0] => ClassA.construct
          [2] => ClassB.construct
          [3] => ClassB.Set
          )


          as SetA() is never invoked



          When you invoked the sequence you described, $classA and $classB are two different instances, so you will never get what you expect.



          To get what you want, you need to do this:



          $classB = new ClassB();
          $classB->SetB();
          $classB->SetA();
          print_r($classB->property);





          share|improve this answer




























            up vote
            3
            down vote













            You misunderstand how inheritance works in general: $classA is an instance of ClassA and has nothing to do with the instance $classB of ClassB you have generated.



            An instance of ClassB inherits all public and protected properties and methods of ClassA but as long as you don't use them, you will not see them.



            And all instances, whether from ClassA or from ClassB, are unrelated to each other, they only have the same "template" but each has its own property values.






            share|improve this answer























            • on other hands there's no possibility for classB to access to values of $property defined on classA. Isn't it?
              – Stefano Radaelli
              Nov 14 at 8:29






            • 1




              @StefanoRadaelli Yes there is, if the property if public or protected or if there is a setter in ClassA to set a private property. However, the property is still specific for that instance of ClassB, it does not change anything in other objects of ClassA or ClassB.
              – jeroen
              Nov 14 at 8:36


















            up vote
            1
            down vote













            That's really simple: why did you expect the parent constructor to run if you forgot to call it in ClassB? According to https://3v4l.org/keJ2a, this has not changed since PHP 5.0.0 and still works the same in recent PHP 7 versions






            share|improve this answer

















            • 2




              Also you need to call SetA() somewhere in your class B instance to get [1] => ClassA.Set in your array.
              – Daniyal Nasir
              Nov 14 at 8:23




















            up vote
            1
            down vote













            If you want to get the expected result then you need to change the code as per the PHP oops concept this will not work as you want.



            You Updated Code



            Class ClassA {
            public $property = array();
            function __construct() {
            $this->property = "ClassA.construct";
            $this->SetA();
            }
            public function SetA() {
            $this->property = "ClassA.Set";
            }
            }

            Class ClassB extends ClassA {
            function __construct() {

            parent::__construct();//invoke parent constructor
            $this->property = "ClassB.construct";
            }
            function SetB() {
            $this->property = "ClassB.Set";
            }
            }


            $classB = new ClassB();
            $classB->SetB();
            print_r($classB->property);


            Expected result:



            Array
            (
            [0] => ClassA.construct
            [1] => ClassA.Set
            [2] => ClassB.construct
            [3] => ClassB.Set
            )


            When parent::__construct(); invokes then it maintains $property array variable for child class also.



            Note: As we know OOPS concept, every object has a different instance.






            share|improve this answer























            • That's clear. But in this case ClassA->SetA() method cannot be called upon certain condition. My goal is to (#1) instanciate ClassA with some values from __construct, (#2) then call main.php -> ClassA->SetA() if "a certain condition is true" and (#3) then to allow to ClassB to get all the values of $property already set from ClassA.
              – Stefano Radaelli
              Nov 14 at 9:13












            • Yes, you are right but can not go against rules. Also, we can not merge two instances into a single one.
              – Gufran Hasan
              Nov 14 at 9:21











            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53295703%2fphp-property-inheritance-on-nested-class-structure%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            4 Answers
            4






            active

            oldest

            votes








            4 Answers
            4






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote



            accepted










            In PHP, parent constructors aren't called automatically, to get your behaviour you need to do the following:



            Class ClassB extends ClassA {
            function __construct() {
            parent::__construct();
            $this->property = "ClassB.construct";
            }
            function SetB() {
            $this->property = "ClassB.Set";
            }
            }


            And, at most, you'll get this



            Array
            (
            [0] => ClassA.construct
            [2] => ClassB.construct
            [3] => ClassB.Set
            )


            as SetA() is never invoked



            When you invoked the sequence you described, $classA and $classB are two different instances, so you will never get what you expect.



            To get what you want, you need to do this:



            $classB = new ClassB();
            $classB->SetB();
            $classB->SetA();
            print_r($classB->property);





            share|improve this answer

























              up vote
              1
              down vote



              accepted










              In PHP, parent constructors aren't called automatically, to get your behaviour you need to do the following:



              Class ClassB extends ClassA {
              function __construct() {
              parent::__construct();
              $this->property = "ClassB.construct";
              }
              function SetB() {
              $this->property = "ClassB.Set";
              }
              }


              And, at most, you'll get this



              Array
              (
              [0] => ClassA.construct
              [2] => ClassB.construct
              [3] => ClassB.Set
              )


              as SetA() is never invoked



              When you invoked the sequence you described, $classA and $classB are two different instances, so you will never get what you expect.



              To get what you want, you need to do this:



              $classB = new ClassB();
              $classB->SetB();
              $classB->SetA();
              print_r($classB->property);





              share|improve this answer























                up vote
                1
                down vote



                accepted







                up vote
                1
                down vote



                accepted






                In PHP, parent constructors aren't called automatically, to get your behaviour you need to do the following:



                Class ClassB extends ClassA {
                function __construct() {
                parent::__construct();
                $this->property = "ClassB.construct";
                }
                function SetB() {
                $this->property = "ClassB.Set";
                }
                }


                And, at most, you'll get this



                Array
                (
                [0] => ClassA.construct
                [2] => ClassB.construct
                [3] => ClassB.Set
                )


                as SetA() is never invoked



                When you invoked the sequence you described, $classA and $classB are two different instances, so you will never get what you expect.



                To get what you want, you need to do this:



                $classB = new ClassB();
                $classB->SetB();
                $classB->SetA();
                print_r($classB->property);





                share|improve this answer












                In PHP, parent constructors aren't called automatically, to get your behaviour you need to do the following:



                Class ClassB extends ClassA {
                function __construct() {
                parent::__construct();
                $this->property = "ClassB.construct";
                }
                function SetB() {
                $this->property = "ClassB.Set";
                }
                }


                And, at most, you'll get this



                Array
                (
                [0] => ClassA.construct
                [2] => ClassB.construct
                [3] => ClassB.Set
                )


                as SetA() is never invoked



                When you invoked the sequence you described, $classA and $classB are two different instances, so you will never get what you expect.



                To get what you want, you need to do this:



                $classB = new ClassB();
                $classB->SetB();
                $classB->SetA();
                print_r($classB->property);






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 14 at 8:25









                kitensei

                1,33512354




                1,33512354
























                    up vote
                    3
                    down vote













                    You misunderstand how inheritance works in general: $classA is an instance of ClassA and has nothing to do with the instance $classB of ClassB you have generated.



                    An instance of ClassB inherits all public and protected properties and methods of ClassA but as long as you don't use them, you will not see them.



                    And all instances, whether from ClassA or from ClassB, are unrelated to each other, they only have the same "template" but each has its own property values.






                    share|improve this answer























                    • on other hands there's no possibility for classB to access to values of $property defined on classA. Isn't it?
                      – Stefano Radaelli
                      Nov 14 at 8:29






                    • 1




                      @StefanoRadaelli Yes there is, if the property if public or protected or if there is a setter in ClassA to set a private property. However, the property is still specific for that instance of ClassB, it does not change anything in other objects of ClassA or ClassB.
                      – jeroen
                      Nov 14 at 8:36















                    up vote
                    3
                    down vote













                    You misunderstand how inheritance works in general: $classA is an instance of ClassA and has nothing to do with the instance $classB of ClassB you have generated.



                    An instance of ClassB inherits all public and protected properties and methods of ClassA but as long as you don't use them, you will not see them.



                    And all instances, whether from ClassA or from ClassB, are unrelated to each other, they only have the same "template" but each has its own property values.






                    share|improve this answer























                    • on other hands there's no possibility for classB to access to values of $property defined on classA. Isn't it?
                      – Stefano Radaelli
                      Nov 14 at 8:29






                    • 1




                      @StefanoRadaelli Yes there is, if the property if public or protected or if there is a setter in ClassA to set a private property. However, the property is still specific for that instance of ClassB, it does not change anything in other objects of ClassA or ClassB.
                      – jeroen
                      Nov 14 at 8:36













                    up vote
                    3
                    down vote










                    up vote
                    3
                    down vote









                    You misunderstand how inheritance works in general: $classA is an instance of ClassA and has nothing to do with the instance $classB of ClassB you have generated.



                    An instance of ClassB inherits all public and protected properties and methods of ClassA but as long as you don't use them, you will not see them.



                    And all instances, whether from ClassA or from ClassB, are unrelated to each other, they only have the same "template" but each has its own property values.






                    share|improve this answer














                    You misunderstand how inheritance works in general: $classA is an instance of ClassA and has nothing to do with the instance $classB of ClassB you have generated.



                    An instance of ClassB inherits all public and protected properties and methods of ClassA but as long as you don't use them, you will not see them.



                    And all instances, whether from ClassA or from ClassB, are unrelated to each other, they only have the same "template" but each has its own property values.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 14 at 8:37

























                    answered Nov 14 at 8:23









                    jeroen

                    81.5k1698122




                    81.5k1698122












                    • on other hands there's no possibility for classB to access to values of $property defined on classA. Isn't it?
                      – Stefano Radaelli
                      Nov 14 at 8:29






                    • 1




                      @StefanoRadaelli Yes there is, if the property if public or protected or if there is a setter in ClassA to set a private property. However, the property is still specific for that instance of ClassB, it does not change anything in other objects of ClassA or ClassB.
                      – jeroen
                      Nov 14 at 8:36


















                    • on other hands there's no possibility for classB to access to values of $property defined on classA. Isn't it?
                      – Stefano Radaelli
                      Nov 14 at 8:29






                    • 1




                      @StefanoRadaelli Yes there is, if the property if public or protected or if there is a setter in ClassA to set a private property. However, the property is still specific for that instance of ClassB, it does not change anything in other objects of ClassA or ClassB.
                      – jeroen
                      Nov 14 at 8:36
















                    on other hands there's no possibility for classB to access to values of $property defined on classA. Isn't it?
                    – Stefano Radaelli
                    Nov 14 at 8:29




                    on other hands there's no possibility for classB to access to values of $property defined on classA. Isn't it?
                    – Stefano Radaelli
                    Nov 14 at 8:29




                    1




                    1




                    @StefanoRadaelli Yes there is, if the property if public or protected or if there is a setter in ClassA to set a private property. However, the property is still specific for that instance of ClassB, it does not change anything in other objects of ClassA or ClassB.
                    – jeroen
                    Nov 14 at 8:36




                    @StefanoRadaelli Yes there is, if the property if public or protected or if there is a setter in ClassA to set a private property. However, the property is still specific for that instance of ClassB, it does not change anything in other objects of ClassA or ClassB.
                    – jeroen
                    Nov 14 at 8:36










                    up vote
                    1
                    down vote













                    That's really simple: why did you expect the parent constructor to run if you forgot to call it in ClassB? According to https://3v4l.org/keJ2a, this has not changed since PHP 5.0.0 and still works the same in recent PHP 7 versions






                    share|improve this answer

















                    • 2




                      Also you need to call SetA() somewhere in your class B instance to get [1] => ClassA.Set in your array.
                      – Daniyal Nasir
                      Nov 14 at 8:23

















                    up vote
                    1
                    down vote













                    That's really simple: why did you expect the parent constructor to run if you forgot to call it in ClassB? According to https://3v4l.org/keJ2a, this has not changed since PHP 5.0.0 and still works the same in recent PHP 7 versions






                    share|improve this answer

















                    • 2




                      Also you need to call SetA() somewhere in your class B instance to get [1] => ClassA.Set in your array.
                      – Daniyal Nasir
                      Nov 14 at 8:23















                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    That's really simple: why did you expect the parent constructor to run if you forgot to call it in ClassB? According to https://3v4l.org/keJ2a, this has not changed since PHP 5.0.0 and still works the same in recent PHP 7 versions






                    share|improve this answer












                    That's really simple: why did you expect the parent constructor to run if you forgot to call it in ClassB? According to https://3v4l.org/keJ2a, this has not changed since PHP 5.0.0 and still works the same in recent PHP 7 versions







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 14 at 8:21









                    Nico Haase

                    2,43591932




                    2,43591932








                    • 2




                      Also you need to call SetA() somewhere in your class B instance to get [1] => ClassA.Set in your array.
                      – Daniyal Nasir
                      Nov 14 at 8:23
















                    • 2




                      Also you need to call SetA() somewhere in your class B instance to get [1] => ClassA.Set in your array.
                      – Daniyal Nasir
                      Nov 14 at 8:23










                    2




                    2




                    Also you need to call SetA() somewhere in your class B instance to get [1] => ClassA.Set in your array.
                    – Daniyal Nasir
                    Nov 14 at 8:23






                    Also you need to call SetA() somewhere in your class B instance to get [1] => ClassA.Set in your array.
                    – Daniyal Nasir
                    Nov 14 at 8:23












                    up vote
                    1
                    down vote













                    If you want to get the expected result then you need to change the code as per the PHP oops concept this will not work as you want.



                    You Updated Code



                    Class ClassA {
                    public $property = array();
                    function __construct() {
                    $this->property = "ClassA.construct";
                    $this->SetA();
                    }
                    public function SetA() {
                    $this->property = "ClassA.Set";
                    }
                    }

                    Class ClassB extends ClassA {
                    function __construct() {

                    parent::__construct();//invoke parent constructor
                    $this->property = "ClassB.construct";
                    }
                    function SetB() {
                    $this->property = "ClassB.Set";
                    }
                    }


                    $classB = new ClassB();
                    $classB->SetB();
                    print_r($classB->property);


                    Expected result:



                    Array
                    (
                    [0] => ClassA.construct
                    [1] => ClassA.Set
                    [2] => ClassB.construct
                    [3] => ClassB.Set
                    )


                    When parent::__construct(); invokes then it maintains $property array variable for child class also.



                    Note: As we know OOPS concept, every object has a different instance.






                    share|improve this answer























                    • That's clear. But in this case ClassA->SetA() method cannot be called upon certain condition. My goal is to (#1) instanciate ClassA with some values from __construct, (#2) then call main.php -> ClassA->SetA() if "a certain condition is true" and (#3) then to allow to ClassB to get all the values of $property already set from ClassA.
                      – Stefano Radaelli
                      Nov 14 at 9:13












                    • Yes, you are right but can not go against rules. Also, we can not merge two instances into a single one.
                      – Gufran Hasan
                      Nov 14 at 9:21















                    up vote
                    1
                    down vote













                    If you want to get the expected result then you need to change the code as per the PHP oops concept this will not work as you want.



                    You Updated Code



                    Class ClassA {
                    public $property = array();
                    function __construct() {
                    $this->property = "ClassA.construct";
                    $this->SetA();
                    }
                    public function SetA() {
                    $this->property = "ClassA.Set";
                    }
                    }

                    Class ClassB extends ClassA {
                    function __construct() {

                    parent::__construct();//invoke parent constructor
                    $this->property = "ClassB.construct";
                    }
                    function SetB() {
                    $this->property = "ClassB.Set";
                    }
                    }


                    $classB = new ClassB();
                    $classB->SetB();
                    print_r($classB->property);


                    Expected result:



                    Array
                    (
                    [0] => ClassA.construct
                    [1] => ClassA.Set
                    [2] => ClassB.construct
                    [3] => ClassB.Set
                    )


                    When parent::__construct(); invokes then it maintains $property array variable for child class also.



                    Note: As we know OOPS concept, every object has a different instance.






                    share|improve this answer























                    • That's clear. But in this case ClassA->SetA() method cannot be called upon certain condition. My goal is to (#1) instanciate ClassA with some values from __construct, (#2) then call main.php -> ClassA->SetA() if "a certain condition is true" and (#3) then to allow to ClassB to get all the values of $property already set from ClassA.
                      – Stefano Radaelli
                      Nov 14 at 9:13












                    • Yes, you are right but can not go against rules. Also, we can not merge two instances into a single one.
                      – Gufran Hasan
                      Nov 14 at 9:21













                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    If you want to get the expected result then you need to change the code as per the PHP oops concept this will not work as you want.



                    You Updated Code



                    Class ClassA {
                    public $property = array();
                    function __construct() {
                    $this->property = "ClassA.construct";
                    $this->SetA();
                    }
                    public function SetA() {
                    $this->property = "ClassA.Set";
                    }
                    }

                    Class ClassB extends ClassA {
                    function __construct() {

                    parent::__construct();//invoke parent constructor
                    $this->property = "ClassB.construct";
                    }
                    function SetB() {
                    $this->property = "ClassB.Set";
                    }
                    }


                    $classB = new ClassB();
                    $classB->SetB();
                    print_r($classB->property);


                    Expected result:



                    Array
                    (
                    [0] => ClassA.construct
                    [1] => ClassA.Set
                    [2] => ClassB.construct
                    [3] => ClassB.Set
                    )


                    When parent::__construct(); invokes then it maintains $property array variable for child class also.



                    Note: As we know OOPS concept, every object has a different instance.






                    share|improve this answer














                    If you want to get the expected result then you need to change the code as per the PHP oops concept this will not work as you want.



                    You Updated Code



                    Class ClassA {
                    public $property = array();
                    function __construct() {
                    $this->property = "ClassA.construct";
                    $this->SetA();
                    }
                    public function SetA() {
                    $this->property = "ClassA.Set";
                    }
                    }

                    Class ClassB extends ClassA {
                    function __construct() {

                    parent::__construct();//invoke parent constructor
                    $this->property = "ClassB.construct";
                    }
                    function SetB() {
                    $this->property = "ClassB.Set";
                    }
                    }


                    $classB = new ClassB();
                    $classB->SetB();
                    print_r($classB->property);


                    Expected result:



                    Array
                    (
                    [0] => ClassA.construct
                    [1] => ClassA.Set
                    [2] => ClassB.construct
                    [3] => ClassB.Set
                    )


                    When parent::__construct(); invokes then it maintains $property array variable for child class also.



                    Note: As we know OOPS concept, every object has a different instance.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 14 at 9:21

























                    answered Nov 14 at 8:32









                    Gufran Hasan

                    3,30331325




                    3,30331325












                    • That's clear. But in this case ClassA->SetA() method cannot be called upon certain condition. My goal is to (#1) instanciate ClassA with some values from __construct, (#2) then call main.php -> ClassA->SetA() if "a certain condition is true" and (#3) then to allow to ClassB to get all the values of $property already set from ClassA.
                      – Stefano Radaelli
                      Nov 14 at 9:13












                    • Yes, you are right but can not go against rules. Also, we can not merge two instances into a single one.
                      – Gufran Hasan
                      Nov 14 at 9:21


















                    • That's clear. But in this case ClassA->SetA() method cannot be called upon certain condition. My goal is to (#1) instanciate ClassA with some values from __construct, (#2) then call main.php -> ClassA->SetA() if "a certain condition is true" and (#3) then to allow to ClassB to get all the values of $property already set from ClassA.
                      – Stefano Radaelli
                      Nov 14 at 9:13












                    • Yes, you are right but can not go against rules. Also, we can not merge two instances into a single one.
                      – Gufran Hasan
                      Nov 14 at 9:21
















                    That's clear. But in this case ClassA->SetA() method cannot be called upon certain condition. My goal is to (#1) instanciate ClassA with some values from __construct, (#2) then call main.php -> ClassA->SetA() if "a certain condition is true" and (#3) then to allow to ClassB to get all the values of $property already set from ClassA.
                    – Stefano Radaelli
                    Nov 14 at 9:13






                    That's clear. But in this case ClassA->SetA() method cannot be called upon certain condition. My goal is to (#1) instanciate ClassA with some values from __construct, (#2) then call main.php -> ClassA->SetA() if "a certain condition is true" and (#3) then to allow to ClassB to get all the values of $property already set from ClassA.
                    – Stefano Radaelli
                    Nov 14 at 9:13














                    Yes, you are right but can not go against rules. Also, we can not merge two instances into a single one.
                    – Gufran Hasan
                    Nov 14 at 9:21




                    Yes, you are right but can not go against rules. Also, we can not merge two instances into a single one.
                    – Gufran Hasan
                    Nov 14 at 9:21


















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53295703%2fphp-property-inheritance-on-nested-class-structure%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

                    How to change which sound is reproduced for terminal bell?

                    Can I use Tabulator js library in my java Spring + Thymeleaf project?