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

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"; }
}
?>

Related

Determine the name of the calling class (parent or child) in parent class method

Looking for a clean way to determine the class (in this case, either parent or child class) of the method that calls a method in the parent class.
I thought late static binding could handle this, but seems like that only really works for calling a static method directly, and not from within an instantiated object's method.
Consider the following:
abstract class ParentClass {
public function parentMethod() {
self::_log("parent.non.static");
}
public static function parentStatic() {
self::_log("parent.static");
}
public static function getClassName() {
return __CLASS__;
}
protected static function _log($key) {
$prefix = 'graphite.key.prefix';
$class = static::getClassName(); // gets the object's class, not calling class
$g_key = "{$prefix}.{$class}.{$key}";
echo "{$g_key} \n";
// Graphite::increment($g_key);
}
}
class ChildClass extends ParentClass {
public function childMethod() {
self::_log("child.non.static");
}
public static function childStatic() {
self::_log("child.static");
}
public static function getClassName() {
return __CLASS__;
}
}
$obj = new ChildClass;
$obj->childMethod(); // graphite.key.prefix.ChildClass.child.non.static
$obj->parentMethod(); // graphite.key.prefix.ChildClass.parent.non.static
ParentClass::parentStatic(); // graphite.key.prefix.ParentClass.parent.static
ChildClass::childStatic(); // graphite.key.prefix.ChildClass.child.static
Looking for a clean way to get the class that calls the _log() method without having to pass it in as a parameter. Doesn't have to be static at all, but I was playing around with the late static binding, because I thought that would work, but it just gets the name of the instantiated object, not the child/parent class of the method that calls the _log() method :-/
Edit:
Just to be clear, I'm after getting the class name of the method that called _log() from within the instantiated object (like parentMethod() and childMethod()) Don't care if _log() is static or not. If that makes it easier, fine. But the static ParentClass::parentStatic() and ChildClass::childStatic() were just to show late static bindings and what I figured might work, but not from calling within an instantiated object
http://php.net/manual/en/function.get-called-class.php
class One {
public static function test() {
echo get_called_class() . PHP_EOL;
}
}
class Two extends One {}
One::test();
Two::test();
Output:
One
Two
Also, according to the top comment in the docs static::class also works as of PHP 5.5.
get_class will get the class name of a class instance. This can also be called on $this within a class. If you have a class that extends/implements another, $this will refer the the instantiated class, meaning the child class.
Another option is to use debug_backtrace to get the stack of functions that lead up to where you currently are. You can parse the returned array to get whatever you need including line numbers, classes, functions, methods, whatever.

different between static:: and $this-> and which syntax i should use?

I have some code:
class a {
public static function getCl() {
echo __CLASS__;
}
public function test() {
static::getCl();
}
}
class b extends a {
public static function getCl() {
echo __CLASS__;
}
}
$testClass = new b();
$testClass->test();
and this result : b. Then i try this:
class a {
public static function getCl() {
echo __CLASS__;
}
public function test() {
$this->getCl();
}
}
class b extends a {
public static function getCl() {
echo __CLASS__;
}
}
$testClass = new b();
$testClass->test();
this result is still b. I already know the different between static:: and self:: but can someone show me what is the different between static:: and $this-> in my code. Which one should i use?
Your context will produce the same result.
Here is simply description about both.
static:- refers late static binding As of PHP 5.3.0, PHP implements a feature called late static bindings which can be used to reference the called class in a context of static inheritance.
Static references to the current class like self:: or CLASS are resolved using the class in which the function belongs, as in where it was defined:
While Late static bindings tries to solve that limitation by introducing a keyword that references the class that was initially called at runtime. Basically, a keyword that would allow you to reference child class from parent class method. It was decided not to introduce a new keyword but rather use static that was already reserved.
$this:- refers current object.
Once inside an object's function, you have complete access to its variables, but to set them you need to be more specific than just using the variable name you want to work with. To properly specify you want to work with a local variable, you need to use the special $this variable, which PHP always sets to point to the object you are currently working with.

late static binding | without modifying parent class with `static` keyword

I have following parent and child class.
class Parent_class {
protected static function method_one() {
echo "I am in Parent_class in method_one";
}
protected function execute() {
static::method_one();
}
public function start() {
$this->execute();
}
}
class Child_class extends Parent_class {
protected static function method_one() {
echo "I am in Child_class in method_one";
}
}
$obj = new Child_class();
$obj->start();
Result - it is calling Child class method.
The result is as expected because of static late binding is supported in php5.3 with the already reserved keyword static.
But the issue is, I do not have write access to Parent class, hence I can not use static while calling methode_one and hence it is not performing late static binding.
Is there any way out using which I can access overriding method ?
Parent class is a defined library, and I can not modify it.
Way out is to modify the parent class or drop this thought completely, but can you suggest any other alternative ?
Why not implement execute or start in child class?

When I extend a class, do I call the static functions directly from a subtype or should I use parent:: each time?

When I defined a function in a supertype and called without parent:: it gave me and error teling me it's undefined function. I am wondering if I should use parent:: each time or if I am doing something wrong somewhere else.
I have a class, named core, which has an escape() function for escaping strings
I am trying to call this function from subtypes.
all methods are static.
Right now I don'T think static methods are inherited. I call all the static superclass methods with
parent::mystaticmethod()
now. Because static methods are not inherited.
use parent:: only when you are going to override function in your child class
Best way to explain this is this example:
class Parent {
function test1() {}
function test2() {}
function __construct() {}
}
class Child extends Parent {
function test1() {} // function is overrided
function test3() {
parent::test1(); // will use Parent::test1()
$this->test1(); // will use Child::test1()
$this->test2(); // will use Parent:test2()
}
function __construct() {
parent::__construct() // common use of parent::
... your code.
}
}
Practical example (static methods):
class LoaderBase {
static function Load($file) {
echo "loaded $file!<br>";
}
}
class RequireLoader extends LoaderBase {
static function Load($file) {
parent::Load($file);
require($file);
}
}
class IncludeLoader extends LoaderBase {
static function Load($file) {
parent::Load($file);
include($file);
}
}
LoaderBase::Load('common.php'); // this will only echo text
RequireLoader::Load('common.php'); // this will require()
IncludeLoader::Load('common.php'); // this will include()
Output:
loaded common.php!
loaded common.php!
loaded common.php!
Anyways using parent:: is more useful in non-static methods.
As of PHP 5.3.0, PHP implements a feature called late static bindings which can be used to reference the called class in a context of static inheritance.
More information here http://php.net/manual/en/language.oop5.late-static-bindings.php

call a static method inside a class?

how do i call a static method from another method inside the same class?
$this->staticMethod();
or
$this::staticMethod();
self::staticMethod();
More information about the Static keyword.
Let's assume this is your class:
class Test
{
private $baz = 1;
public function foo() { ... }
public function bar()
{
printf("baz = %d\n", $this->baz);
}
public static function staticMethod() { echo "static method\n"; }
}
From within the foo() method, let's look at the different options:
$this->staticMethod();
So that calls staticMethod() as an instance method, right? It does not. This is because the method is declared as public static the interpreter will call it as a static method, so it will work as expected. It could be argued that doing so makes it less obvious from the code that a static method call is taking place.
$this::staticMethod();
Since PHP 5.3 you can use $var::method() to mean <class-of-$var>::; this is quite convenient, though the above use-case is still quite unconventional. So that brings us to the most common way of calling a static method:
self::staticMethod();
Now, before you start thinking that the :: is the static call operator, let me give you another example:
self::bar();
This will print baz = 1, which means that $this->bar() and self::bar() do exactly the same thing; that's because :: is just a scope resolution operator. It's there to make parent::, self:: and static:: work and give you access to static variables; how a method is called depends on its signature and how the caller was called.
To see all of this in action, see this 3v4l.org output.
This is a very late response, but adds some detail on the previous answers
When it comes to calling static methods in PHP from another static method on the same class, it is important to differentiate between self and the class name.
Take for instance this code:
class static_test_class {
public static function test() {
echo "Original class\n";
}
public static function run($use_self) {
if($use_self) {
self::test();
} else {
$class = get_called_class();
$class::test();
}
}
}
class extended_static_test_class extends static_test_class {
public static function test() {
echo "Extended class\n";
}
}
extended_static_test_class::run(true);
extended_static_test_class::run(false);
The output of this code is:
Original class
Extended class
This is because self refers to the class the code is in, rather than the class of the code it is being called from.
If you want to use a method defined on a class which inherits the original class, you need to use something like:
$class = get_called_class();
$class::function_name();
In the later PHP version self::staticMethod(); also will not work. It will throw the strict standard error.
In this case, we can create object of same class and call by object
here is the example
class Foo {
public function fun1() {
echo 'non-static';
}
public static function fun2() {
echo (new self)->fun1();
}
}
call a static method inside a class
className::staticFunctionName
example
ClassName::staticMethod();

Categories