Access Static var from multiplie class inheritance (PHP) - php

How can I access $var from C class, i try with parent::parent::$var but this didn't work.
<?php
class A {
protected static $var = null;
public function __construct(){
self::$var = "Hello";
}
}
class B extends A {
parent::__construct();
//without using an intermediate var. e.g: $this->var1 = parent::$var;
}
class C extends B {
//need to access $var from here.
}
?>

Since the variable is static you can access the variable like below -
A::$var;

As long as the property isn't declared as private, then you can just use self::. Anything other than private properties are available from anywhere in the hierarchy.
class A {
protected static $var = null;
public function __construct(){
self::$var = "Hello";
}
}
class B extends A {
}
class C extends B {
function get() { echo self::$var; }
}
(new C)->get();
Hello
See https://eval.in/1022037

Related

Multiple inheritance + Multi-Level inheritance in PHP: Is there a pattern or possibility to solve constructor inheritance?

Have a look at the following example:
class A {
protected $a;
public function __construct() {
$this->a = "foo";
}
}
trait Q {
protected $q;
public function __construct() {
$this->q = "happy";
}
}
class B extends A {
use Q;
protected $b;
public function __construct() {
$this->b = "bar";
}
}
trait X {
protected $x;
public function __construct() {
$this->x = "lorem";
}
}
class C extends B {
use X;
protected $c;
public function __construct() {
$this->c = "sure";
}
public function giveMeEverything() {
echo $this->a." ".$this->b." ".$this->c." ".$this->x." ".$this->q;
}
}
$c = new C();
$c->giveMeEverything();
This works just fine - the output is:
sure
The thing is that I want all classes and and traits within the tree to initialize their member variables. Desired output:
foobarsureloremhappy
It must not be solved with constructors! I just want the member variables to be populated on initialization, but I still had no good idea how to solve this. In a real world example this is more complex, therefore please do not $a = "foo"; just within the variables declaration.
The problem is that traits cannot be instantiated so __construct() is kind of meaningless.
The best approach is to initialize your member variables using the class constructor; that's why constructors exist.
If you want to initialize some members that are declared in a trait, then have a trait function and call it in the appropriate class constructor, example:
trait Q {
protected $a;
public function initQ() { $this->a = "whatever"; }
}
class MyClass {
use Q;
public function __construct() {
$this->initQ();
}
}

How to make an abstract variable static to a sub class

<?php
abstract class A {
public static $var = "A";
public function setVar() {
}
public function test() {
$this->setVar();
echo static::$var;
}
public function returnVar() {
return static::$var;
}
}
class B extends A {
public function setVar() {
static::$var = 'B';
}
}
class C extends A {
public function setVar() {
static::$var = 'C';
}
}
$class = new B();
$class->test();
$class2 = new C();
$class2->test();
echo "</br>";
echo $class->returnVar();
echo $class2->returnVar();
?>
What I'm trying to do is make the variable $var static to the class that extends abstract class A without having to re-declare it else where.
So say perhaps I create multiple objects from class B that extends A, I want all objects made from class B to share the same $var value.
Say I then create objects based on class C, they should all share the same value of $var...
This is the result I'm currently getting:
BC
CC
However, what I'm looking for is:
BC
BC
Thanks
Try it like that:
#setting
public function setVar() {
static::$var[get_class($this)] = 'B';
}
#getting in abstract
public function returnVar() {
return static::$var[get_class($this)];
}
#add this in the abstract class
public function setVar();

How to make the inherited class run a method from same class in PHP

Whats wrong with me OOP here.
I want to inherit from Class A
The return_output method will do something common so I don't want to write that in the inherited classes.
However when I do B->return_output() I want it to run the do_something method in Class B, but I see that it always runs the method from Class A.
Should I replace $this with something else?
class A {
private function do_something() {
// do something
}
public function return_output() {
$op = $this->do_something();
// add some wrappers to $op
return $op;
}
}
class B extends A {
private function do_something() {
// do something different
}
}
var newClass = new B;
echo B->return_output();
use protected and not private since you are running it inside of scope a and scope b can't access private scope a:
class A {
protected function do_something() {
echo('ado_something');
}
public function return_output() {
$op = $this->do_something();
// add some wrappers to $op
return $op;
}
}
class B extends A {
protected function do_something() {
echo('bdo_something');
}
}
$newClass = new B;
echo $newClass->return_output();

Access private properties in a class hierarchy from a common base function

I want to define a method once in a base class and call it in successive constructors in a class hierarchy. Each time is it called I want it to operate on the properties of the class from which it is called.
For example, A is the base class and the method is defined here. B inherits from A, and C inherits from B.
When I instantiate the concrete class C the constructor will call the base class method, and I want it to operate on the properties of C. (This will be a private array which I will populate when I initialise it).
The constructor of C then calls parent::__construct. When the B constructor calls the base class method, the method should operate on the properties of B. Before the B constructor is complete it will call parent::_construct, and the A constructor will operate on the properties of A.
I was looking into LSB, but it won't work correctly because parent::__construct is a forwarding call. I tried using the result of get_parent_class() in place of parent::, but my calls to static::propertyName error out because propertyName is not a constant.
How can I do this?
EDIT: Here's a code example. The code below outputs "P Q P Q P Q". I want it to output "P Q R S T U".
class A {
private $property = array('P','Q');
function __construct() {
$this->myMethod();
}
public function myMethod() {
foreach ($this->property as $value) {
echo $value . " ";
}
}
}
class B extends A {
private $property = array('R','S');
function __construct()
{
parent::__construct();
$this->myMethod();
}
}
class C extends B {
private $property = array('T','U');
function __construct()
{
parent::__construct();
$this->myMethod();
}
}
$c = new C();
As far as I know it is not possible. In case of private the variable is not available to method and in case of public/protected its overwritten.
You can get your desired result by passing
$this->property
in each myMethod call like this -
$this->myMethod($this->property)
and changing your myMethod definition accordingly.
If I got you right, this is your example:
class A {
public function __construct() {
}
public function someMethod($arg) {
$someProperty = $arg;
}
}
class B extends A {
public function __construct() {
parent::__construct();
}
}
class C extends B {
private $someProperty;
public function __construct() {
parent::__construct();
}
}
Everything works as you would expect. There is only one problem; class A has no property someProperty. If you want to use it in class A, you have to define it there. If you want to use it in subclasses, you have to make it protected. Your class A has therefore to look like this:
class A {
protected $someProperty;
public function __construct() {
}
public function someMethod($arg) {
$this->someProperty = $arg;
}
}
Now you can use it in class B and C, and class A can use the property with someMethod:
$instance = new C();
$instance->someMethod("test");
Because your example has $property defined as private and you never override myMethod, when an object of type C or B is instantiated, it runs class A's constructor, which calls myMethod() from class A where the value of $property is array('P', 'Q');.
If you want it to print 'P Q R S T U' you'll have to override myMethod() and call it from each individual class' __construct() method, OR declare $property to be protected so that the child classes can overwrite its value.
Something like this:
class A {
private $property = array('P','Q');
function __construct() {
$this->myMethod();
}
public function myMethod() {
foreach ($this->property as $value) {
echo $value . " ";
}
}
}
class B extends A {
private $property = array('R','S');
function __construct()
{
parent::__construct();
$this->myMethod();
}
public function myMethod() {
foreach ($this->property as $value) {
echo $value . " ";
}
}
}
class C extends B {
private $property = array('T','U');
function __construct()
{
parent::__construct();
$this->myMethod();
}
public function myMethod() {
foreach ($this->property as $value) {
echo $value . " ";
}
}
}
$c = new C();
OR THIS:
class A {
protected $property = array('P','Q');
function __construct() {
$this->myMethod();
}
public function myMethod() {
foreach ($this->property as $value) {
echo $value . " ";
}
}
}
class B extends A {
protected $property = array('R','S');
function __construct()
{
parent::__construct();
$this->myMethod();
}
}
class C extends B {
protected $property = array('T','U');
function __construct()
{
parent::__construct();
$this->myMethod();
}
}
$c = new C();
proptery cannot be overloaded in child classes. Once you have a C, it does not have the private properties of A anymore. It has no way to access them (outside of reflection). The only way you would be able to do this would be to pass the properties to someMethod in a chain, but this requires access to the desired property.
Using protected would not work either because then the children would just override the parent property.
The solution to this is not have have C be an a (is-a) but have C have the properties of an A (has-a .. composition). This would require some rework of your hierarchy, though, and your calls would have to be more explicit. Something like
class C {
private $b;
private $properties = array('T', 'U');
public function __construct(B $b) {
$this->b = $b;
}
public function someMethod() {
$this->b->someMethod($properties);
}
}
class B {
private $properties = array('R', 'S');
private $a;
public function __construct(A $a) {
$this->a = $a;
}
public function someMethod($properties) {
$this->a->someMethod(array_merge($this->properties, $properties));
}
}
class A {
private $properties = array('P', 'Q');
public function someMethod($properties) {
//your implementation plus an array_merge
}
}
This obviously increases verbosity in your definitions, which is bad. You may want to reconsider why you have to do some things the way you are doing them. You may be able to get around this with traits (assuming you have PHP 5.4)

PHP Accessing Parent Class Variable

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'
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.
Just echo it since it's inherited
echo $this->bb;
With parent::$bb; you try to retrieve the static constant defined with the value of $bb.
Instead, do:
echo $this->bb;
Note: you don't need to call parent::_construct if B is the only class that calls it. Simply don't declare __construct in B class.
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::$this->bb; //works by M
}
}
$test = new B($some);
$test->childfunction();`
$bb has now become the member of class B after extending class A.
So you access $bb like it's an attribute of class B.
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 $this->bb;
}
}
$test = new B($some);
$test->childfunction();
all the properties and methods of the parent class is inherited in the child class so theoretically you can access them in the child class but beware using the protected keyword in your class because it throws a fatal error when used in the child class.
as mentioned in php.net
The visibility of a property or method
can be defined by prefixing the
declaration with the keywords public,
protected or private. Class members
declared public can be accessed
everywhere. Members declared protected
can be accessed only within the class
itself and by inherited and parent
classes. Members declared as private
may only be accessed by the class that
defines the member.
PHP Accessing Parent Class Protected Variable & Methods
class A {
protected $bb = 'parent bb';
protected function sayHello(){
echo 'Say Hello';
}
}
class B extends A {
public function childfunction() {
echo $this->bb.'<br>';
echo $this->sayHello();
}
}
$test = new B();
$test->childfunction();
Through parent class contructor you can pass data to parent class from child class. Have a look below example for better understanding
<?php
class Student
{
public $name;
function __construct($name){
$this->name = $name;
}
}
class Test extends Student
{
public $age;
function __construct($name,$age){
$this->age = $age;
parent::__construct($name);
}
}
$obj = new Test("sajib khan" ,21);
echo $obj->name;
echo $obj->age;
?>
class A {
private $points = 100;
public function getPoints() {
return $this->points;
}
}
class B extends A {
protected $points = 70;
public function getPoints() {
return parent::getPoints();
}
}
$element = new B();
echo $element->getPoints();
change the visibility private or protected for test

Categories