I have the following stucture
class Foo
{
public static $a = "parent";
public static function Load()
{
return static::$a;
}
public function Update()
{
return self::$a;
}
}
class Bar extends Foo
{
private static $a = "child";
}
I want the Update function to be able to return $a aswell, but I can't get it to work.
Bar::Load(); //returns child, Correct.
$bar = new Bar();
$bar->Update(); //returns parent, Wrong.
I've tried self:: , static:: and get_class() without success.
Change self::$a in update()
class Foo
{
protected static $a = "parent"; // Notice this is now "protected"
public function child()
{
return static::$a;
}
public function parent()
{
return self::$a;
}
}
class Bar extends Foo
{
protected static $a = "child"; // Notice this is now "protected"
}
$bar = new Bar();
print $bar->child() . "\n";
print $bar->parent() . "\n";
See my code
class Foo
{
protected static $a = "parent";
public static function Load()
{
return static::$a;
}
public function Update()
{
return static::$a;
}
}
class Bar extends Foo
{
protected static $a = "child";
}
Bar::Load(); //returns child, Correct.
$bar = new Bar();
$bar->Update(); //returns child, Correct.
Related
I am noob in PHP because I am mostly do .NET/Java. In code base I am working, I have,
class SomeOtherBaseClass{
public $prop2;
public function __construct(string $prop3)
{
$this->prop2 = $prop3;
}
public function __toString()
{
return $this->prop2 . ' '. $this->prop2;
}
}
class SomeClass
{
public function __toString()
{
return $this->prop1 . ' '. $this->prop1;
}
public $prop1;
public function someMethod() : SomeOtherBaseClass
{
return $this->createClass();
}
public function __construct()
{
$this->prop1 = 'foo';
}
private function createClass(
): SomeOtherBaseClass {
return new class(
$this->prop1
) extends SomeOtherBaseClass {
};
}
}
$class = new SomeClass();
echo $class;
echo $class->someMethod();
Why I am getting error that prop1 not found. Clearly createClass function is part of SomeClass which have prop1. Why I cannot access prop1 inside createClass?
It's because $prop1 has no value or meaning.
You can add a __construct() function to resolve your issue:
public function __construct()
{
$this->prop1 = 'foo';
}
now when you call this class (e.g. $foo = new SomeClass();):
$prop1 has a value of foo which can be used in your functions:
public function echoProp()
{
echo $this->prop1; # will output foo
}
Note: This is just an explanation answer - not a copy/paste solution - but the principles are all here for you to use in your code.
Let me know if this wasn't what you were looking for :)
Edit:
if prop1 exists in SomeOtherClass, when you construct you can do
public function __construct()
{
$this->class = new SomeClass();
$this->prop1 = $this->class->prop1;
}
class A{
const MY_CONSTANT = 'my constant';
}
class B{
protected $a;
public function __construct(A $a)
{
$this->a = $a;
}
public function someFunction()
{
return $this->a::MY_CONSTANT;
}
}
Why the constant is not accessible like this way - $this->a::MY_CONSTANT? Anybody knows any other ways?
The above can be achieved in this way. Here we are using get_class function to get classname as string. which we are storing it in a variable and then retrieve the value of constant by using that variable.
Try this code snippet here
<?php
ini_set('display_errors', 1);
class A{
const MY_CONSTANT = 'my constant';
}
class B{
protected $a;
public function __construct(A $a)
{
$this->a = $a;
}
public function someFunction()
{
$class=get_class($this->a);
echo $class::MY_CONSTANT;
}
}
$object=new B(new A());
$object->someFunction();
You can also do the same by this approach.
class A{
const MY_CONSTANT = 'my constant';
public function __get($key){
$r = new ReflectionObject($this);
if($r->hasConstant($key)){ return $r->getConstant($key); }
}
}
class B{
public function someFunction()
{
return new A();
}
}
$b = new B();
var_dump($b->someFunction()->MY_CONSTANT);
Original Answer Link
I was wondering if it is possible for an object to guess the class of his "owner" if the "owner" have a property of the object class.
Let me explain what I mean by a small example
class A {
public function Magic(){/*return owner's class*/}
}
class B {
private $foo;
public function __construct() {
$this->foo=new A();
}
public function getFoo(){
return $this->foo;
}
}
class C {
private $bar;
public function __construct() {
$this->bar=new A();
}
public function getBar(){
return $this->bar;
}
}
$b= new B();
$c= new C();
print($b->getFoo()->Magic()); // would print B
print($c->getBar()->Magic()); // would print C
I don't know if I am dreaming or if it's possible...
How would you do it if not possible?
You need to inject to A his owner. So, the Magic method is less "magical", but the dependency is more clear.
class A
{
private $owner;
public function __construct($owner)
{
$this->owner = $owner;
}
public function Magic() { return get_class($this->owner); }
}
class B
{
private $foo;
public function __construct()
{
$this->foo = new A($this);
}
public function getFoo()
{
return $this->foo;
}
}
class C
{
private $bar;
public function __construct()
{
$this->bar = new A($this);
}
public function getBar()
{
return $this->bar;
}
}
$b= new B();
$c= new C();
print($b->getFoo()->Magic()); // would print B
print($c->getBar()->Magic()); // would print C
While cloning an object, I need to perform the same initializations that happen during the object construction.
Can I do this?
public class MyClass {
protected $myVar;
public function __construct()
{
$this->myVar = 0
}
public function __clone()
{
$this->__construct();
}
}
You can do that just fine
class MyClass {
protected $myVar;
public function __construct()
{
echo "constructing!\n";
$this->myVar = 0;
}
public function __clone()
{
echo "cloning!\n";
$this->__construct();
}
}
$a = new MyClass();
$b = clone $a;
Output
constructing!
cloning!
constructing!
The following example defines a foo class, which constructs a bar class, and stores it as a property $foo->bar. In the bar class, is it possible to reference the 'false' parent class, and use it's functions?
class bar
{
public function test_false_parent()
{
//Is it possible to access foo->display() from here
{unknown code}::display();
}
}
class foo
{
public $bar;
public function __construct()
{
$this->bar = new bar;
}
public function display()
{
echo "in";
}
}
$foo = new foo;
$foo->bar->test_false_parent();
//Equivalent to $foo->display();
Not without a back reference:
class bar
{
protected $foo;
public function __construct(foo $foo)
{
$this->foo = $foo;
}
public function test_false_parent()
{
$this->foo->display();
}
}
class foo
{
public $bar;
public function __construct()
{
$this->bar = new bar($this);
}
public function display()
{
echo "in";
}
}
$foo = new foo;
$foo->bar->test_false_parent();