Using static variables/methods from with nonstatic calls....
Frankly it seems like a horrible practice from a strict OO perspective, and from the point of view of anyone who wants to write maintainable programs.
Even the java developers agree with this:
Note: You can also refer to static methods with an object reference....
but this is discouraged because it does not make it clear that they are class methods.
http://java.sun.com/docs/books/tutor...classvars.html
You seem to think of Java as the Gold Standard so why are you complaining that PHP implemented the static key word in the way the Java developers wanted to originally? It would seem to me they only allowed object references to static methods as a kludge, a way to defray criticism, and allow lazy designers or programmers inexperienced in OO to jump into OO without having to learn the object structure of the programs they are using.
andr3a wrote:Is this so hard to imagine ?
class String {
private $str = '';
public final function __construct($str) {
$this->str = $str;
}
public final function __toString() {
return $this->str;
}
public final function concat() {
$args = func_get_args();
return new String((isset($this)?$this:'').implode('', $args));
}
}
$str = String::concat("a", "b", "c");
$sts = new String("a");
echo '<pre>';
var_dump(array(
$str,
$sts,
''.$str,
''.$sts,
'$str === $str->concat("b", "c") ?',
''.$str === ''.$sts->concat("b", "c")
));
exit('</pre>');
Works perfectly! ... ooops, I forget the E_STRICT notice ... so if E_STRICT is a good feature, it should shut up because this code isn't replicable with static keyword feature ...
Are You sure, now, static implementation is a bug or a limit and not a feature with PHP 5?
And this is only an example .... now think about a class called File
echo File::getContent("test.txt");
$test = new File("test.txt");
echo $test->getContent();
it's soo much simple to do, impossible to apply respecting E_STRICT and using static keyword ... what a feature!
It's mind boggling why you would want a concat function to do two different things when it is called from an object instance, and when it is called statically.
Functions should have one clear function, and that function shouldn't change based on the calling context!
In your example I could legitimately do something like this:
echo String::concat("b", "c"); // returns bc
$sts = new String("a");
echo $sts.concat("b", "c"); // returns abc
Now it boggles my mind, why I would want to do that. Shouldn't concat return the same thing if I give it the same parameters? Ofcourse it should!
And in PHP with E_STRICT it throws you a helpful notice (not fatal error) so you can rethink your design along proper OO lines.
Then after many long and terrible hours of reasoning, you'll simply start using your function statically and calling it like this.
echo String::concat($sts, "b", "c"); // returns abc
Anytime you think you're FORCED to use a static call from instance object, then you should stop, sit back, and re-think your design. Ask yourself? Do I really need to expose this functionality in this place without knowing what type of object this is?
If the answer is yes... then write a new non-static function to delegate the call to the static reference.
i.e.
class MyObject
{
public function A()
{
return "something that's really really really needs this objects internal data to resolve";
}
public function callB()
{
return self::B();
}
public static function B()
{
return "Something that all objects of this type are capable of doing and only depends on the parameters";
}
}
$foo = new MyObject;
echo $foo->A();
echo $foo->callB();
$i_am_lazy = "return ".get_class($foo)."::B();";
echo eval($i_am_lazy);
Or you can use get_class to fudge your way into knowing the class name.
In either case... if you want to call a method (and you want to really call this method), then that means:
a) You know this method exists on this object
Which implies
b) You know which class this object belongs to
and
c) You know the parameters the method accepts, the expected return value and.... if the method is declared as static or not
Since you know all that... why not just make the static call?