Get child class name php from static function - php

In php I have a ROOT class from which all other classes inherit.
abstract class ROOT{
public static function getClass(){
}
}
I want that function to return the class(name) of the object which inherits from this class. So if I create an object Tree (extends ROOT) and call getClass on it it should say "Tree"
The function get_class() only works on objects, so can't be used inside a static function.
Is there any way to accomplish this?

Instead of get_class(), use get_called_class().

http://www.php.net/manual/en/function.get-called-class.php
abstract class ROOT {
public static function getClass() {
return get_called_class();
}
}
class Tree extends ROOT {
}
$Tree = new Tree();
echo $Tree->getClass(); // Outputs "Tree"

Related

Call a child class from a child class PHP

i am a new PHP, i have some files like this:
In root.php i have
<?php
class Root
{
// Some functions here
}
In child_1.php i have:
class Child_1 extends Root
{
function abc()
{
//Some code here
}
}
In child_2.php i have:
class Child_2 extends Root
{
function cde()
{
// How can i call function abc() from class Child_1 here
}
}
Yes, how can i call a function of Child_1 class from a function of Child_2 class.
As classes that extend one parent (Root) know nothing about each other (and you cannot change this), you can do:
class Chirld_2 extends Root
{
function cde()
{
$obj = new Chirld_1();
$obj->abc();
}
}
But obviously it's a lack of good design. I suppose, you have to define abc() in a Root class to make it available in both children:
class Root
{
function abc()
{
//Some code here
}
}
class Chirld_1 extends Root
{
}
class Chirld_2 extends Root
{
}
$c1 = new Chirld_1();
$c1->abc();
$c2 = new Chirld_2();
$c2->abc();
you can call any function in class_1 or Class_2 of parent class
but within any child class you can't call method from another.because They do not have a relevance
a solution is you call class_2 in constructor of class_1
If you want to use functions of Chirld_1 from Chirld_2 or vice versa without creating an object of the child class, then one option is to declare Chirld_1 and Chirld_2 as traits of the parent class Root. This will allow you to call functions of Chirld_1 from Chirld_2 and vice verse using the $this keyword. The classes should be declared as follows:
<?php
trait Chirld_1
{
public function test1()
{
$this->test2();
}
}
trait Chirld_2
{
public function test2()
{
$this->test1();
}
}
class Root
{
use Chirld_1;
use Chirld_2;
// Some functions here
}
See this links for more information about traits: http://php.net/manual/en/language.oop5.traits.php
Try using parent:: or static:: or self:: notation to designate the class you want to use.
http://php.net/manual/pl/keyword.parent.php

accessing properties of an externally initialized parent class from a child class

A parent class is constructed from outside the child class, thus, it's constructor cannot be called from inside the child. How should one go about accessing properties of the parent from the child in this case.
Example:
class MyParent {
protected $args;
protected $child;
public function MyParent($args=false){
$this->args=$args;
$this->child=new MyChild();
}
public function main(){
$this->child->printArgs();
}
}
class MyChild extends MyParent{
public function MyChild(){}
public function printArgs(){
Echo "args: ".$this->args['key']." = ".$this->args['value']."\n";
}
}
$parent=new MyParent(array('key'=>'value'));
$parent->main();
Empty variables are returned when run:
jgalley#jgalley-debian:~/code/otest$ php run.php
args: =
__construct() is the constructor. You are using a variant from ancient PHP4-times.
You instanciate two completely different objects, therefore of course the property $args is completely independent.
abstract class MyParent {
protected $args;
public function __construct($args=false){
$this->args=$args;
}
public function main(){
$this->printArgs();
}
abstract public function printArgs();
}
class MyChild extends MyParent{
public function printArgs(){
Echo "args: ".$this->args['key']." = ".$this->args['value']."\n";
}
}
$$object=new MyChild(array('key'=>'value'));
$object->main();
This at least works, but a problem is, that I don't know exactly what are the design goals. Because it seems to be a kind of cli-Application you should have a look at existing solutions to get an idea, how it could get solved.

Confused about abstract class in php

If I have an abstract class like this:
abstract class MyApp
{
public function init()
{
$this->stuff = $this->getStuff();
}
public function getStuff()
{
return new BlueStuff();
}
}
And then I have a class that extends from this abstract class like this:
class MyExtendedClass extends MyApp
{
public function init()
{
parent::init();
}
public function getStuff()
{
return new RedStuff();
}
}
If I do:
$myobj = new MyExtendedClass();
$myobj->init();
Why does the method getStuff from the child class get called? Isn't $this in the context of the abstract class? If so, shouldn't the method of the abstract class get called?
Thanks!
New answer
In PHP you can use subclasses as if all methods in the parent class that don't exist in the subclass have been copied to the subclass. So your example would be the same as:
class MyExtendedClass extends MyApp {
public function init() {
$this->stuff = $this->getStuff();
}
public function getStuff() {
return new RedStuff();
}
}
Just think of the subclass as having all code of the parent class and you're normally all right. There is one exception to this rule: properties. A private property of a class can only be accessed by that class, subclasses can't access the private properties of parent classes. In order to do that you need to change the private property into a protected property.
Original answer
Abstract classes in PHP are just like regular classes with one big difference: they can't get initiated. This means you can't do new AbstractClass();.
Since they work exactly like regular classes for everything else, this also is the case for extending classes. This means that PHP first tries to find the method in the initiated class, and only looks in the abstract classes if it doesn't exist.
So in your example this would mean that the getStuff() method from MyExtendedClass is called. Furthermore, this means you can leave out the init() method in MyExtendedClass.

accessing a child class prop from parent

I mean something like that:
class parentClass {
public function method() {
echo $this->prop;
}
}
class childClass extends parentClass {
public $prop = 5;
}
How can I get a child prop from the parent prop?
Thanks...
Either I don't fully understand what you want or the solution is as trivial as the following code.
class parentClass {
public function method() {
echo $this->prop;
}
}
class childClass extends parentClass {
public $prop = 5;
}
$object = new childClass();
$object->method();
I mean the child class is extending the base class which means it will also inherit all the methods of its parent's class. That makes the whole process of using the parent's class method as simple as calling it from the instance of the child class.
All protected and public members of child classes are visible from within their parent class in PHP, so the example code you provided should work just fine. Quote from the php doc:
Members declared protected can be accessed only within the class
itself and by inherited and parent classes.
But the actual question is: do you really need it?
The proper OO way would be to define a self-contained parent class that expresses something. It should not need to access properties of child classes - this is a so-called code smell. If you really think that you have a case where a similar construct is necessary, you are probably looking for abstract methods, which guarantee that every child class has this property:
abstract class Animal {
public function makeNoise() {
echo $this->getNoiseString();
}
protected abstract function getNoiseString();
}
class Cat extends Animal {
protected function getNoiseString() {
return 'meow';
}
}
//parent
class parentClass {
protected $prop = null;
public function method() {
echo $this->prop;
}
}
//child
class childClass extends parentClass {
protected $prop = 5;
}
Make sure the variable is defined in the parentclass as well. So it will be accessible by the parent.

How do I call a static child function from parent static function?

How do I call child function from parent static function ?
In php5.3 there is a built in method called get_called_class() to call child method from parent class. But my server is running with php 5.1.
Is there any way can do this ?
I want to call it from a static function . So that I can not use "$this"
So i should use "self" keyword.
Below example my parent class is "Test123" , from the parent class static function "myfunc" am trying to call child class function like this "self::test();"
abstract class Test123
{
function __construct()
{
// some code here
}
public static function myfunc()
{
self::test();
}
abstract function test();
}
class Test123456 extends Test123
{
function __construct()
{
parent::__construct();
}
function test()
{
echo "So you managed to call me !!";
}
}
$fish = new Test123456();
$fish->test();
$fish->myfunc();
Edit: What you try to achieve is not possible with PHP 5.1. There is no late static bindings PHP Manual in PHP 5.1, you need to explicitly name the child class to call the child function: Test123456::test(), self will be Test123 in a static function of the class Test123 (always) and the static keyword is not available to call a static function in PHP 5.1.
Related: new self vs new static; PHP 5.2 Equivalent to Late Static Binding (new static)?
If you are referring to a static parent function, then you need to explicitly name the parent (or child) for the function call in php 5.1:
parentClass::func();
Test123456::test();
In PHP 5.3 you can do this instead with the static keyword PHP Manual to resolve the called class' name:
static::func();
static::test();
If those are non-static, just use $this PHP Manual:
$this->parentFunc();
$this->childFunc();
Or if it has the same name, use parent PHP Manual:
parent::parentFunc();
(which is not exactly what you asked for, just putting it here for completeness).
Get_called_class() has been introduced for very specific cases like to late static bindings PHP Manual.
See Object Inheritance PHP Manual
I suspect you are a bit confused abuot parent / child, class / object and function / method.
IonuČ› G. Stan has provided the explanation of how to invoke a method which is not declared in a parent class (which as he says should be abstract or implement the __call() method).
However if you mean how do invoke a method which has been overridden in a child class from the parent, then it is not possible - nor should it be. Consider:
Class shape {
...
}
Class circle extends shape {
function area() {
}
}
Class square extends shape {
function area() {
}
}
If it is your intent to call the area method on an instance of 'shape' (which does not have an area method) then which child should it use? Both the child methods would depend on properties which are not common / not implemented by the shape class.
try this:
<?php
class A {
public static function newInstance() {
$rv = new static();
return $rv;
}
public function __construct() { echo " A::__construct\n"; }
}
class B extends A {
public function __construct() { echo " B::__construct\n"; }
}
class C extends B {
public function __construct() { echo " C::__construct\n"; }
}
?>

Categories