samhuri.net/posts/2006/07/late-static-binding.md
Sami Samhuri 281272aa32
Normalize post tags onto a small canonical taxonomy
Existing Tags fields were messy (mixed case, duplicates, lots of
one-off tags) and 28 posts had none at all. Normalized casing,
deduped, mapped common synonyms (textmate/xcode/git -> tools, os x
variants -> mac-os-x, etc) onto a canonical set, and added 1-2
canonical tags to every previously untagged post so most posts now
carry at least one useful tag. Specific extra tags were kept
alongside the canonical ones rather than stripped.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-21 20:15:53 -07:00

1.6 KiB

Title Author Date Timestamp Tags
Late static binding Sami Samhuri 19th July, 2006 2006-07-19T10:23:00-07:00 php, coding

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.