Posted by sjs
on Friday, July 21
Update: This has been discussed for PHP6. A little late, but I guess better than never.
I made a mistake while I was coding, for shame! Anyway this particular mistake was that I invoked a class method on the wrong class. The funny part was that this method was an instance method in the class which I typed by mistake. In the error log I saw something like “Invalid use of $this in class function.”
I knew for a fact I hadn’t used $this in a class method, so it was kind of a confusing error. I went to the file in question and found out that it was calling an instance method as a class method. Now that is some crazy shit.
I would fully expect the PHP parser to give me an error like “No class method [foo] in class [blah]”, rather than try and execute it as a class method. The syntax is completely different; you use :: to call a class method and -> to call an instance method. And you use the name of a class when you call a class method.
This code:
class Foo {
public static function static_fun()
{
return "This is a class method!\n";
}
public function not_static()
{
return "This is an instance method!\n";
}
}
echo '<pre>';
echo "From Foo:\n";
echo Foo::static_fun();
echo Foo::not_static();
echo "\n";
echo "From \$foo = new Foo():\n";
$foo = new Foo();
echo $foo->static_fun();
echo $foo->not_static();
echo '</pre>';
Produces:
From Foo:
This is a class method!
This is an instance method!
From $foo = new Foo():
This is a class method!
This is an instance method!
What the fuck?! http://www.php.net/manual/en/language.oop5.static.php is lying to everyone.
Posted by sjs
on Wednesday, July 19
Update: This has been discussed and will be uh, sort of fixed, in PHP6. You’ll be able to use static::my_method() to get the real reference to self in class methods. Not optimal, but still a solution I guess.
As colder on ##php (freenode) told me today, class methods in PHP don’t have what they call late static binding. What’s that? It means that this code:
class Foo
{
public static function my_method()
{
echo "I'm a " . get_class() . "!\n";
}
}
class Bar extends Foo
{}
Bar::my_method();
outputs “I’m a Foo!”, instead of “I’m a Bar!”. That’s not fun.
Using __CLASS__ in place of get_class() makes zero difference. You end up with proxy methods in each subclass of Foo that pass in the real name of the calling class, which sucks.
class Bar extends Foo
{
public static function my_method()
{
return parent::my_method( get_class() );
}
}
I was told that they had a discussion about this on the internal PHP list, so at least they’re thinking about this stuff. Too bad PHP5 doesn’t have it. I guess I should just be glad I won’t be maintaining this code.
The resident PHP coder said “just make your code simpler”, which is what I was trying to do by removing duplication. Too bad that plan sort of backfired. I guess odd things like this are where PHP starts to show that OO was tacked on as an after-thought.
Posted by sjs
on Sunday, July 16
It’s true. I’m sitting here coding in PHP using the Zend Framework and all I can think about is how much nicer Rails is, or how much easier it is to do [x] in Ruby. It’s not that the Zend Framework is bad or anything, it’s quite nice, but you just can’t match Ruby’s expressiveness in a language like PHP. Add the amazing convenience Rails builds on top of Ruby and that’s a really hard combo to compete with.
Posted by sjs
on Thursday, July 06
At Seekport I’m currently working on an app to handle the config of their business-to-business search engine. It’s web-based and I’m using PHP, since that’s what they’re re-doing the front-end in. Right now it’s a big mess of Perl, the main developer (for the front-end) is gone, and they’re having trouble managing it. I have read through it, and it’s pretty dismal. They have config mixed with logic and duplicated code all over the place. There’s an 1100 line sub in one of the perl modules. Agh!