The following code is Example #4 from the PHP documentation on late static bindings.
<?php
class A {
public static function foo() {
static::who();
}
public static function who() {
echo __CLASS__."\n";
}
}
class B extends A {
public static function test() {
A::foo();
parent::foo();
self::foo();
}
public static function who() {
echo __CLASS__."\n";
}
}
class C extends B {
public static function who() {
echo __CLASS__."\n";
}
}
C::test();
My question is why the output is "ACC" but not "ABC". Expecting your helps!
I think you have less knowledges about "non-forwarding call".Access to the link and find what you need.
Related
In the documentation is this example and understand it without problems
class Bar{
public function test() {
$this->testPrivate();
$this->testPublic();
}
public function testPublic() {
echo "Bar::testPublic\n";
}
private function testPrivate() {
echo "Bar::testPrivate\n";
}
}
class Foo extends Bar{
public function testPublic() {
echo "Foo::testPublic\n";
}
private function testPrivate() {
echo "Foo::testPrivate\n";
}
}
$myFoo = new foo();
$myFoo->test();
The result is:
Bar::testPrivate
Foo::testPublic
But now redefine the test () method in the class foo
class Bar{
public function test() {
echo '<br>Im Bar::test';
$this->testPrivate();
$this->testPublic();
}
public function testPublic() {
echo "<br>Bar::testPublic\n";
}
private function testPrivate() {
echo "<br>Bar::testPrivate\n";
}
}
class Foo extends Bar{
public function test() {
echo '<br>Im Foo::test';
$this->testPrivate();
$this->testPublic();
}
public function testPublic() {
echo "<br>Foo::testPublic\n";
}
private function testPrivate() {
echo "<br>Foo::testPrivate\n";
}
}
$myFoo = new Foo();
$myFoo->test();
The result is:
Im Foo::test
Foo::testPrivate
Foo::testPublic
php allows me to override the private method testPrivate (), Why?
Why not? It's difficult to answer that question as it's just how PHP works. If you want to prohibit your methods from being overwritten then you can use the final keyword.
Additionally, in your example, if you do not declare the private method within Foo, you will get an error as Foo technically has no definition for that method. Extending classes have no visibility of any private properties or methods within their parent class.
I have a base class A:
class A {
public static function a() {
...
}
public static function b() {
...
}
}
and an extended class B
class B extends A {
public static function a() {
...
}
public static function c() {
...
}
}
I would like to be able to call all the methods using B::
How would I call A::b, using B::?
You should be able to accomplish this as easily as:
class B extends A {
public static function a() {
parent::a();
}
}
See the docs
Is there any meaning to it?
public static function a(){
static::_b();
}
private static function _b(){
}
static here is same as self in any situation, because you cannot override a private function in the child class, right?
As Germann Arlington said in the comment. A subclass can have it's own implementation of _b() - even with a different visibility.
<?php
class Foo {
public static function a() { static::_b(); }
private static function _b() { echo 'Foo'; }
}
class FooEx extends Foo {
public static function _b() { echo 'FooEx'; }
}
FooEx::a();
prints FooEx
I'm trying to accomplish this without requiring a function on the child class... is this possible? I have a feeling it's not, but I really want to be sure...
<?php
class A {
public static function who() {
echo __CLASS__;
}
public static function test() {
static::who(); // Here comes Late Static Bindings
}
}
class B extends A {
public static function who() {
echo __CLASS__;
}
}
B::test(); //returns B
?>
Use get_called_class() instead of __CLASS__. You'll also be able to replace static with self as the function will resolve the class through late binding for you:
class A {
public static function who() {
echo get_called_class();
}
public static function test() {
self::who();
}
}
class B extends A {}
B::test();
I'd like to start by showing a test case:
class A {
public static $instance=null;
public function __construct(){
self::$instance=$this;
}
public function className(){
return get_class(self::$instance);
}
}
class B extends A {
public function className(){
return get_class(self::$instance);
}
}
// test code
$b=new B();
echo $b->className; // B
$a=new A();
echo $a->className; // A
echo $b->className; // A <- error: not B any more!
Notes
I'm using a factory+singleton patterns above. Well, somewhat.
I don't need any specs on "implementing patterns correctly". I need problem solving, not KISS violations ;).
Critics might say A should be an interface. Ideally, that's what it should have been, but it's just a simple class, sorry.
The issues lies in the fact that self::$instance is the same for all instances. How do I separate self::$instance for each class?
Edit: I've had this idea:
$GLOBALS['store']=array();
class A {
public static $instance=null;
public function __construct(){
$GLOBALS['store'][__CLASS__]=$this;
}
}
You could store an instance per class name:
class A {
public static function getInstance(){
// Maybe use this function to implement the singleton pattern ...
return self::$instance[get_called_class()];
}
public function className(){
return get_class(self::getInstance());
}
}
You can not do this the clean way.
That is one of the mayor drawbacks on stati propertys: you cannot overrride them.
But you wantet an sollution so.....here is the worarround:
use __calllStatic
<?php
class A {
public static function __callstatic($name,$args)
{
if($name="getClass"){
return 'A';
}
}
}
class B extends A{
public static function __callstatic($name,$args)
{
if($name="getClass"){
return 'B';
}
}
}
echo A::getClass();
echo B::getClass();
?>
the output of this is "AB";
You can add a public static $instance=null; declaration in class B.
class A {
public static $instance=null;
public function __construct(){
self::$instance=$this;
}
public function className(){
return get_class(self::$instance);
}
}
class B extends A {
public static $instance=null;
public function className(){
return get_class(self::$instance);
}
}
// test code
$b=new B();
echo $b->className(); // B
$a=new A();
echo $a->className(); // A
echo $b->className(); // Now returns B, as desired.