Properties shared between parent and parent classes in php

class parents{ public $a; function __construct(){ echo $this->a; } } class child extends parents{ function __construct(){ $this->a = 1; parent::__construct(); } } $new = new child();//print 1 

This code is above print 1, which means that whenever we create an instance of a child class and assign a value to properties inherited from its parent, a property in its parent class is also assigned. But the code below shows different:

 class parents{ public $a; function test(){ $child = new child(); echo $this->a; } } class child extends parents{ function __construct(){ $this->a = 1; } } $new = new parents(); $new->test();//print nothing 

Where I assign a value to its child class, and the parent instance does not have the value that it assigned to the child class, why?

Thanks!

-one
source share
5 answers

In the above example, since the constructor function is called from a child class, it processes the object, which is used as if it were a child object, which simply uses the function in the parent class, as if it were native.

In the bottom example, you have two separate objects acting independently. The parent has $ a as well as a child, but they do not match $ a because they are contained in separate objects. so when you print $ this-> a in the parent class, this refers to the parent instance of $ a, whereas if you echo $ a after setting $ this-> a = 1 in the child class, it will display the child instance of $ a.

Hope this clears something for you.

0
source

This is because in the first example, you create an instance of child

 $new = new child();//print 1 

and in the second you create parents

 $new = new parents(); 

The second works the same as the first, using $ new = new child ();

If you want to access $ a by creating an instance of child (), you need to do it like this:

 class parents{ public $a; function test(){ $child = new child(); //CHANGE HERE echo $child->a; } } class child extends parents{ function __construct(){ $this->a = 1; } } $new = new parents(); $new->test(); 
0
source

When you create a parent instance, the created child instance extends the parent class to the test() function. However, this does not change the value of $a .

0
source

This happens because you have two different instances that have nothing in common (except inheritance ..) Exceptional behavior can be generated using inner classes that php does not support.

If you want to share each instance of var accros, you must make it static

 class parents{ public static $a; function test(){ $child = new child(); echo self::$a; } } class child extends parents{ function __construct(){ self::$a = 1; } } $new = new parents(); $new->test(); 

which is probably not what you want. Or you will say exactly where you want to change your var

 class parents{ public $a; function test(){ $child = new child(); echo $child->a; } } class child extends parents{ function __construct(){ $this->a = 1; } } $new = new parents(); $new->test(); 
0
source

You mix object composition and class inheritance.

Inheritance (implemented through the extends keyword) defines the relationship is a .

Composition defines the connection has a .

To illustrate this concept, we start with inheritance.

 class Person { public $name; public function talk(){}; public function poop(){}; } class Parent extends Person { public function __construct($name) { $this->name = $name; } } class Child extends Person { public function __construct($name){ $this->name = $name; } } 

In this example, we define a class things called people. From this definition, we get two different subtypes of "People, Parents, and Children." When we subtype a class, the subtype gets its own copy of all properties and has access to all methods defined in the base type, therefore, without its definition, the Child and the Parent have a name and can both speak and poop as well as be a person.

For instance:

 $parent = new Parent("Homer"); $child = new Child("Bart"); $parent->talk(); $child->poop(); 

Composition is used when you want to realize a has a ship relationship. Let's review our definition of type Parent.

 class Parent extends Person { public $children = array(); public function __construct($name) { $this->name = $name; } public function addChild(Child $child){ $this->children[] = $child; } } 

What we now allow if for a parent to have a child.

 $parent = new Parent("Homer"); $child = new Child("Bart"); $parent->addChild($child); // now I can access homers first child echo $parent->children[0]->name; 
0
source

Source: https://habr.com/ru/post/1494778/


All Articles