Asked  7 Months ago    Answers:  5   Viewed   34 times
class A {
    private $aa;
    protected $bb = 'parent bb';

    function __construct($arg) {
       //do something..
    }

    private function parentmethod($arg2) {
       //do something..
    }
}

class B extends A {
    function __construct($arg) {
        parent::__construct($arg);
    }
    function childfunction() {
        echo parent::$bb; //Fatal error: Undefined class constant 'bb' 
    }
}

$test = new B($some);
$test->childfunction();

Question: How do I display parent variable in child? expected result will echo 'parent bb'

 Answers

39
echo $this->bb;

The variable is inherited and is not private, so it is a part of the current object.


Here is additional information in response to your request for more information about using parent:::

Use parent:: when you want add extra functionality to a method from the parent class. For example, imagine an Airplane class:

class Airplane {
    private $pilot;

    public function __construct( $pilot ) {
        $this->pilot = $pilot;
    }
}

Now suppose we want to create a new type of Airplane that also has a navigator. You can extend the __construct() method to add the new functionality, but still make use of the functionality offered by the parent:

class Bomber extends Airplane {
    private $navigator;

    public function __construct( $pilot, $navigator ) {
        $this->navigator = $navigator;

        parent::__construct( $pilot ); // Assigns $pilot to $this->pilot
    }
}

In this way, you can follow the DRY principle of development but still provide all of the functionality you desire.

Wednesday, March 31, 2021
 
petersaints
answered 7 Months ago
49

As your Child class is extending your Parent class, every properties and methods that are either public or protected in the Parent class will be seen by the Child class as if they were defined in the Child class -- and the other way arround.

When the Child class extends the Parent class, it can be seen as "Child is a Parent" -- which means the Child has the properties of the Parent, unless it redefines those another way.

(BTW, note that "parent" is a reserved keyword, in PHP -- which means you can't name a class with that name)


Here's a quick example of a "parent" class :

class MyParent {
    protected $data;
    public function __construct() {
        $this->someMethodInTheParentClass();
    }
    protected function someMethodInTheParentClass() {
        $this->data = 123456;
    }
}

And it's "child" class :

class Child extends MyParent {
    public function __construct() {
        parent::__construct();
    }
    public function getData() {
        return $this->data; // will return the $data property 
                            // that's defined in the MyParent class
    }
}

That can be used this way :

$a = new Child();
var_dump($a->getData());

And you'll get as output :

int 123456

Which means the $data property, defined in the MyParent class, and initialized in a method of that same MyParent class, is accessible by the Child class as if it were its own.


To make things simple : as the Child "is a" MyParent, it doesn't need to keep a pointer to... itself ;-)

Wednesday, March 31, 2021
 
KouiK
answered 7 Months ago
63

include global $myarray at the start of setvalue() function.

public function setvalue() {
    global $myarray;
    $myvalue = $myarray[0];
}

UPDATE:
As noted in the comments, this is bad practice and should be avoided.
A better solution would be this: https://stackoverflow.com/a/17094513/3407923.

Wednesday, March 31, 2021
 
SubniC
answered 7 Months ago
14

Assuming your outer class is called Outer, from the scope of the inner class(non-static), Outer.this.foo to get at the field.

For example,

Outer.this.foo=new ArrayList<>();

where Outer is the name of the class and foo identifies the field.

You can also grab it directly as foo=new Baz() but it'll pick the inner field if there's a naming conflict due to shadowing.

if it's a static inner class, you need an explicit instance:

outerInstance.foo=new ArrayList<>();

or if the field to access is static, access it as usual with:

Outer.staticFoo=new ArrayList<>();
Tuesday, June 29, 2021
 
RompelStompel
answered 4 Months ago
51

property_exists( mixed $class , string $property )

if (property_exists($ob, 'a')) 

isset( mixed $var [, mixed $... ] )

if (isset($ob->a))

isset() will return false if property is null

Example 1:

$ob->a = null
var_dump(isset($ob->a)); // false

Example 2:

class Foo
{
   public $bar = null;
}

$foo = new Foo();

var_dump(property_exists($foo, 'bar')); // true
var_dump(isset($foo->bar)); // false
Saturday, July 31, 2021
 
qitch
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :