I need to know what kind invokes a static method, without sending as parameter
class foo
{
public static function test($clase)
{
echo "Class invoke:" . FUNCTION();
}
}
class A { public function x { foo::test(); } }
class B { public function y { foo::test(); } }
class C { public function z { foo::test(); } }
You can use late static bindings and get_called_class() (PHP >= 5.3) if you make all of your classes extend foo, like this:
class foo
{
public static function test($clase)
{
echo "Class invoke:" . get_called_class();
}
}
class A extends foo { public function x() { self::test(''); } }
class B extends foo { public function y() { self::test(''); } }
class C extends foo { public function z() { self::test(''); } }
With these objects:
$a = new A; $a->x();
$b = new B; $b->y();
$c = new C; $c->z();
You'll get as output:
Class invoke:A
Class invoke:B
Class invoke:C
Related
class A
{
public function child1()
{
$var1 = 'abc';
}
}
class B extends A
{
public function child1()
{
echo parent::$var1; // Return error undefined class constant 'var1'
}
}
How can i access $var1 in this situation ?
Expected result: 'abc'
First you cannot do class B extends class A. The correct syntax would be class B extends A:
class A
{
public function child1()
{
$var1 = 'abc';
return $var1;
}
}
class B extends A
{
public function child1()
{
echo parent::child1();
}
}
$temp = new B;
$temp->child1();
Now what I've done is return the $var1 in your class A.
You cannot call echo parent::$var1; because it is inside a function, so you call the parent function echo parent::child1();.
working example here
You need to make $var1 a class property. See following code:
<?php
class A
{
protected $var1;
public function child1()
{
$this->var1 = 'abc';
}
}
class B extends A
{
public function child1()
{
parent::child1();
echo $this->var1;
}
}
$b = new B();
$b->child1();
?>
I have some classes:
class A{
public $D;
public function __construct()
{
$this->D = new D();
}
}
class C extends E{
public function testC(){
return 'test C';
}
}
class B extends A
{
public function __construct()
{
$this->C = new C();
}
public function testB()
{
echo $this->C->testC();
}
}
(new B)->testB();
I would like transfer variables class A to class C, how i can make it (that i can use variables class A in class C) ?
What you want to achieve is most likely this.
class A
{
public $variable;
}
class B extends A
{
public function __construct()
{
$this->variable = 'value';
}
public function testVar()
{
echo $this->variable;
}
}
I suggest reading OOP basics, there is much more to it than this.
I have 3 classes:
Class A - Parent Class
Class B - Child Class
Class C - Class to be used in Class A
I want to use functions from class C using variables from my Child class.
<?php
class A
{
public function __construct()
{
$this->load();
}
public function load()
{
$class = new C();
$class->test = $this->test;
$this->c = $class;
}
}
class B extends A
{
public function __construct()
{
parent::__construct();
}
}
class C
{
public function display()
{
echo $this->test;
}
}
$b = new B();
$b->test = 1;
$b->c->display();
Your problem is here:
$class->test = $this->test;
You are attempting to use a property that is not yet defined, because when you do this:
$b->test = 1;
the constructor has already been called, and there's nothing in your classes to update C with the value of B's test property.
You can solve this in a couple of different ways.
1) Send the value in B's constructor, and pass it down the entire chain:
class A
{
public function __construct($test)
{
$this->load($test);
}
public function load($test)
{
$class = new C();
$class->test = $test;
$this->c = $class;
}
}
class B extends A
{
public function __construct($test)
{
parent::__construct($test);
}
}
class C
{
public function display()
{
echo $this->test;
}
}
$b = new B(123);
$b->c->display();
2) Add a method to B that will update C's property:
<?php
class A
{
public function __construct()
{
$this->load();
}
public function load()
{
$class = new C();
$this->c = $class;
}
}
class B extends A
{
public function __construct()
{
parent::__construct();
}
public function setTest($test)
{
$this->c->test = $test;
}
}
class C
{
public function display()
{
echo $this->test;
}
}
$b = new B();
$b->setTest(123);
$b->c->display();
Or perhaps a combination of both.
consider this code:
class C
{
public function get()
{
echo 'C';
static::get();
}
public function save()
{
self::get();
}
}
class B extends C
{
public function get()
{
echo 'B';
static::get();
}
}
class A extends B
{
public function get()
{
echo 'A';
}
}
$x = new A();
$x->save();
it echoes CA while I was expected CBA
To get this to work the way you want, reverse the logic - get your save to call the static::get() so it will start at the top of the inheritence tree; and use calls to parent::get() in each inherited class in the tree (except the base level) before echoing its own output
class C
{
public function get()
{
echo 'C';
}
public function save()
{
static::get();
}
}
class B extends C
{
public function get()
{
parent::get();
echo 'B';
}
}
class A extends B
{
public function get()
{
parent::get();
echo 'A';
}
}
$x = new A();
$x->save();
Demo
interface A
{
public function method1();
public function method2();
}
abstract class B implements A
{
public $publicc = 2;
public function method1()
{
echo "in method1 of B<br>";
}
}
class C extends B
{
public $publicc = 4;
public function __construct()
{
}
public function method2()
{
}
public function method1()
{
echo $this->publicc + parent::$publicc; // error for using parent::$publicc
}
}
$obj = new C();
$obj->method1();
But php throws error echo $this->publicc + parent::$publicc. I just want to get parent class $publicc property directly that has value 2, Without using any accessor method. Is there a way to do this in php?
It depends on what publicc exactly holds but a constant might suit your needs?
interface A
{
public function method1();
public function method2();
}
abstract class B implements A
{
const PUBLICC = 2;
public function method1()
{
echo "in method1 of B<br>";
}
}
class C extends B
{
const PUBLICC = 4;
public function __construct()
{
}
public function method2()
{
}
public function method1()
{
echo self::PUBLICC + parent::PUBLICC; // error for using parent::PUBLICC
}
}
$obj = new C();
$obj->method1();
I think you want a static property. If it is a property you want to access without an instance, that usually indicates a candidate for a static variable.
abstract class B implements A
{
protected static $publicc = 2;
...
}
class C extends B
{
public $publicc = 4;
public function __construct()
{
}
public function method2()
{
}
public function method1()
{
echo $this->publicc + parent::$publicc; // error for using parent::$publicc
}
}