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.
Related
<?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();
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
I'm new to OO PHP. Got some questions.
class a {
protected function a1() {
...
}
}
class b extends a {
public function b1() {
...
}
}
Let's say we have 2 classes like explained above. I'm calling b's method like example below
class a {
var $b;
function __construct()
{
$b = new b();
}
protected function a1() {
$b->b1();
}
}
class b extends a {
public function b1() {
...
}
}
I know that, it's possible to call parent class'es method from extended class, but I wonder if reverse way is possible? I mean, calling extended classes method from inside parent class (in this case, class b's method from class a) without declaring in __contruct, simply by $this->b();?
Yes, you can call a method in the extending class.
<?php
class a
{
public function a1 ()
{
$this->b1();
}
protected function b1()
{
echo 'This is in the a class<br />';
}
}
class b extends a
{
protected function b1()
{
echo 'This is in the b class<br />';
}
}
class c extends a
{
protected function b1()
{
echo 'This is in the c class<br />';
}
}
$a = new a();
$a->a1();
$b = new b();
$b->a1();
$c = new c();
$c->a1();
?>
This will result in:
This is in the a class
This is in the b class
This is in the c class
You may also be interested in abstract classes http://us3.php.net/manual/en/language.oop5.abstract.php
use Magic methods of PHP __call or __callStatic
Reference
// 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