// 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");
}
}
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.
I'm currently trying to extend a Webshop CMS and am running into a problem. I have the following classes :
class a {
public function doStuff(){
// doing some A stuff
return $something;
}
}
class b extends a {
public function doStuff(){
// doing some B Stuff
$something = parent::doStuff();
return $something;
}
}
class c extends b {
public function doStuff(){
// doing some C stuff
$something = parent::doStuff(); // <= problem here
return $something;
}
}
I can't change class a or b because they are core classes of the cms (prestashop), which is why all my code goes into class c.
I need doing some A stuff, without running doing some B stuff from class b.
I can't extend a directly because there's other code in b that I need.
I can't seem to come up with a clean solution to this problem other than just copy/pasting everything from a->doStuff() and not calling parent::doStuff() at all.
As far as I know, there's no parent::parent:: construct or anything similar.
Does anyone know of a better solution without having to copy/paste everything?
class c extends b {
public function doStuff(){
// doing some C stuff
$something = a::doStuff(); // change here parent to 'a'
return $something;
}
}
I've done testing with this code and it works
<?php
class a {
public function doStuff(){
echo 'A-Stuff<pre>';
return $something;
}
}
class b extends a {
public function doStuff(){
echo 'B-Stuff<pre>';
parent::doStuff();
}
}
class c extends b {
public function doStuff(){
// doing some C stuff
a::doStuff();
echo 'C-Stuff<pre>';
}
}
$c = new c;
$c->doStuff();
this echoes
A-Stuff
C-Stuff
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