2

I am looking for a middle ground between SELF and STATIC, without the unexpected behavior of Late Static Binding.

Below is my code, with practice results and expected results:

<?php

class A {
public function get_self() {
    return new self();
}

public function get_static() {
    return new static();
}
}

class B extends A {}

class C {
public function static_get_a_self() {
    return A::get_self();
}

public function static_get_a_static() {
    return A::get_static();
}

public function var_get_a_self() {
    $a = new A();
    return $a->get_self();
}

public function var_get_a_static() {
    $a = new A();
    return $a->get_static();
}


public function static_get_b_self() {
    return B::get_self();
}

public function static_get_b_static() {
    return B::get_static();
}

public function var_get_b_self() {
    $b = new B();
    return $b->get_self();
}

public function var_get_b_static() {
    $b = new B();
    return $b->get_static();
}
}

$a = new A();
$b = new B();
$c = new C();

echo "Static calls: \n";
echo "B::get_self() ============= " . get_class(B::get_self()) .   " | expected A \n";
echo "B::get_static() =========== " . get_class(B::get_static()) . " | expected B \n";
echo "A::get_static() =========== " . get_class(A::get_static()) . " | expected A \n";
echo "\n";

echo "Object Calls on A: \n";
echo '$a->get_self() ============ ' . get_class($a->get_self()) .   " | expected A \n";
echo '$a->get_static() ========== ' . get_class($a->get_static()) . " | expected A \n";
echo "\n";

echo "Object Calls on B: \n";
echo '$b->get_self() ============ ' . get_class($b->get_self()) .   " | expected A \n";
echo '$b->get_static() ========== ' . get_class($b->get_static()) . " | expected B \n";
echo "\n";

echo "C Object Calls on A: \n";
echo "Internally against A::{call}\n";
echo '$c->static_get_a_self() === ' . get_class($c->static_get_a_self()) .   " | expected A \n";
echo '$c->static_get_a_static() = ' . get_class($c->static_get_a_static()) . " | expected A < Whoa! \n";
echo "Internally against \$a = new A();\n";
echo '$c->var_get_a_self() ====== ' . get_class($c->var_get_a_self()) .      " | expected A \n";
echo '$c->var_get_a_static() ==== ' . get_class($c->var_get_a_static()) .    " | expected A \n";
echo "\n";

echo "C Object Calls on B: \n";
echo "Internally against B::{call}\n";
echo '$c->static_get_b_self() === ' . get_class($c->static_get_b_self()) .   " | expected A \n";
echo '$c->static_get_b_static() = ' . get_class($c->static_get_b_static()) . " | expected B < Whoa! \n";
echo "Internally against \$b = new B();\n";
echo '$c->var_get_b_self() ====== ' . get_class($c->var_get_b_self()) .      " | expected A \n";
echo '$c->var_get_b_static() ==== ' . get_class($c->var_get_b_static()) .    " | expected B \n";
?>

The Results:

Static calls: 
B::get_self() ============= A | expected A 
B::get_static() =========== B | expected B 
A::get_static() =========== A | expected A 

Object Calls on A: 
$a->get_self() ============ A | expected A 
$a->get_static() ========== A | expected A 

Object Calls on B: 
$b->get_self() ============ A | expected A 
$b->get_static() ========== B | expected B 

C Object Calls on A: 
Internally against A::{call}
$c->static_get_a_self() === A | expected A 
$c->static_get_a_static() = C | expected A < Whoa! 
Internally against $a = new A();
$c->var_get_a_self() ====== A | expected A 
$c->var_get_a_static() ==== A | expected A 

C Object Calls on B: 
Internally against B::{call}
$c->static_get_b_self() === A | expected A 
$c->static_get_b_static() = C | expected B < Whoa! 
Internally against $b = new B();
$c->var_get_b_self() ====== A | expected A 
$c->var_get_b_static() ==== B | expected B 

This kind of makes sense, in Late Static Binding the reserved 'static' variable refers to the last Class scope. What I am looking for is a reserved variable that refers to the Top Class in which the variable occurs.

IE: If I extend a RecordObject class with a class called Member, I need a function I can define in RecordObject that will instantiate a new Member (or Page, or Comment) without having to overwrite this function in each class definition. Is this possible?

The issue I am running in to is, say, I am in Member, and I call Page::get($id), which internally creates a new Object and does some magic. Unfortunately, if I use static, that new object is a Member... No dice.

Mike
  • 1,968
  • 18
  • 35
  • How can you call `B::get_self()`, when there is no such a static function in your code? – sybear Sep 26 '13 at 21:55
  • @Jari It wasn't until recently that PHP introduced the static keyword, so PHP had to allow you to call instance methods via the class, basically to imitate a static method. Only reason it still lets you do it is for backwards compatibility iirc. – Supericy Sep 26 '13 at 22:00
  • That is the source of the issue, really. As I am developing against a legacy codebase. I have already hit about 1-2k lines of new/modified code, if I have to dig through the rest of it just to differentiate between static and non-static calls... I am not doing that right now... – Mike Sep 26 '13 at 22:02
  • @Supericy Alright, that makes sense. – sybear Sep 26 '13 at 22:10
  • @tereško - How is this not related to Object Oriented Programming? – Mike Sep 27 '13 at 13:25

1 Answers1

0

Why are you ignoring strict errors?

Just add static keyword to static methods.

class A {
static public function get_self() {
    return new self();
}

static public function get_static() {
    return new static();
}
}

And everything should be as expected.

http://3v4l.org/RWRE9#v530

sectus
  • 15,605
  • 5
  • 55
  • 97
  • Welcome to the party. In a comment on the OP I mentioned I am codeing against a legacy codebase and I can not change the legacy files. I think the request for the option, or at least the desire for the option, has some merit. I now understand why it is not there, and I have coded around it, but it is not an outrageous feature. – Mike Nov 26 '13 at 21:13