I have the following hiearchy:
class A {
public static function getClass() {
return __CLASS__;
}
}
class B extends A {}
class C extends B {}
without overriding getClass() in either B or C, I would like the following output:
echo A::getClass() // A
echo B::getClass() // B
echo C::getClass() // C
Currently, all of the above simply output A. How can I achieve the desired behavior?
Try this.
class A {
public static function getClass() {
return get_called_class();
}
}
class B extends A {}
class C extends B {}
Here's the demo
Related
Please check the example can I use the A function in C like the below example? If not then how can I use the A function in C?
class A {
public function a()
{
return 'hello';
}
}
class B extends A {
//
}
class C extends B {
// Can I use the A function in C?
}
Methods are inherited through any number of nested classes.
class A {
public function a()
{
return 'hello';
}
}
class B extends A {
//
}
class C extends B {
function c() {
echo $this->a();
}
}
$x = new C();
$x->c(); // This will print `Hello`.
echo $x->a(); // So will this.
so far I have this:
class A
{
public function __construct($v1,$v2) {}
}
class B extends A
{
}
class C extends B
{
}
echo (new \ReflectionClass('C'))->getConstructor()->class;
this works like a charm, it produces A as expected. So far so good.
But then a trait came into my way:
trait X
{
public function __construct($fake) {}
}
class A
{
public function __construct($v1,$v2) {}
}
class B extends A
{
use X;
}
class C extends B
{
}
echo (new \ReflectionClass('C'))->getConstructor()->class;
it now produces B ! But I want to know the parameters of A::constructor, not X::constructor! How to dodge it?
I am trying to test Multi level inheritance using 3 classes in reverse order as define below, giving me error of Class B not found.
class A extends B
{
function area_a(){echo "A::hello";}
}
class B extends C
{
function area_c()
{
echo "hiiii";
}
function area_b(){echo "B::hello";}
}
class C
{
function area_c(){echo "C::hello";}
}
$obj=new A;
$obj->area_b();
It should work this way:
<?php
class C {
function area_c(){echo "C::hello";}
}
class B extends C{
function area_c(){
echo "hiiii";
}
function area_b(){echo "B::hello";}
}
class A extends B{
function area_a(){echo "A::hello";}
}
$obj=new A;
echo $obj->area_b();
Regarding the error, when the code is compiling, then it goes to class A and checks that it extends class B and searches above for class B and can't found, hence the error.
// A is the core class
class A{
public $lang;
function sayhi($name){echo "Hi".$name;}
function speak(){echo "Can Speak".$this->lang;}
}
class B {
function TODO(){
echo " Go to work ";
}
}
I DID for now like this:
class C extends B {
function TODO(){
//more implement here
$a = new A();// here I created an instance.
// do any actions for A
$a->sayhi("Newbie");
}
}
BUT I want to class B have all the construct of class A?
so when I DO on class C (just something like this)
class C extends B {
function TODO(){
//more implement here
// I wish I can
sayhi("Newbie");
}
}
Anybody could tell me how can implement this?
don't know php that good, but are you searching for this...
class A {
}
class B extends A{
}
class C extends B{
}
or did i miss something?
class A{
public $lang;
function sayhi($name){echo "Hi".$name;} //here you missed a semicolon
function speak(){echo "Can Speak".$this->lang;}
}
class B extends A {
function TODO(){
echo " Go to work ";
}
}
class C extends B {
function TODO(){
$this->sayhi("Newbie");
}
}
In php, if A extends B, does B's _constrctor() get executed automatically when A is instantiated? or do I have to call parent->_constructor()?
PHP looks for the top-most (closest to the instantiated class) __construct method it can find. It then executes that one only.
Class A {
public function __construct() {
echo "From A";
}
}
Class B extends A {
public function __construct() {
echo "From B";
}
}
Class C extends A {}
Class D extends B {}
Class E extends B {
public function __construct() {
echo "from E";
}
}
new A(); // From A
new B(); // From B
new C(); // From A
new D(); // From B
new E(); // From E
And parent accesses the next one up the list until there are no more (at which point it'll generate an error)...
So, in class E, running parent::__construct() would execute class B's constructor.
In class B, running parent::__construct() would execute class A's constructor.
In class A, running parent::__construct() will generate an error since there is no constructor...
The answer is you have to call it.
A simple test:
class A {
public function __construct() {
echo 'A';
}
}
class B extends A {
public function __construct() {
echo 'B';
}
}
$ab = new B();
Should tell you all you need to know.
You need to call "parent::__construct()" from A's constructor, if A has one. Otherwise you don't need to.
class A {
function __construct() {
echo 5;
}
}
class B_no_Constructor extends A {
}
class B_with_Constructor extends A {
function __construct(){}
}
//try one
//new B_no_Constructor; //outputs 5
//new B_with_Constructor; //outputs nothing