The estate ->foo
will be accessible, because each one belongs to one of them, however ->foo
will have the value NULL
, because simply overwriting a method within the "child method" will need to call the parent method (equivalent to the "super" of Java and Python), in PHP it is like this:
class Bar extends Foo {
public function __construct(
public $bar,
){
parent::__construct($bar);
}
}
Of course, if your wish is to pass the value of $bar
for the parent method, if the goal is another then do so by pointing out the value you want:
class Bar extends Foo {
public function __construct(
public $bar,
){
parent::__construct('qualquer outro valor');
}
}
The point is that simply if the parent class has the need to always run the constructor for some reason of "setting" values, it will be necessary for the child class to do the necessary procedures with parent::
for the superscripted methods.
It is also worth remembering that if your goal is simply something like this:
public function __construct(
public $bar,
){
parent::__construct($bar);
}
Without the constructor doing anything else, it would just pass the value in the same order of parameters or "named parameters" equally, there is no reason to overwrite, just do this:
<?php
class Foo {
public function __construct(
public $foo,
){}
}
class Bar extends Foo {
}
$obj = new Bar('valor');
var_dump($obj->foo); // exibe "valor"
See that we passed the valor
in class Bar()
and was accessible in ->foo
yes, it will be at your disposal.
– novic