PHP4 Static Function Calls

I just noticed that PHP4 does not support any scope modifiers such as static and visibility modifiers like private and public on class members and methods. PHP5 supports all 3, and it took me a several minutes to figure out why my code wouldn’t run on one of the servers. It really creates funny situations. Consider the following example:

class Foo
{
var $one = 1;
function bar()
{
echo "Helo World";
}
function baz()
{
echo $this->one;
}
}

We have and instance variable $one and two functions bar() and baz(). Tell me what happens when I do the following:

$foo = new Foo();
$foo->bar(); // prints Hello World
$foo->baz(); // prints 1
Foo::bar(); // prints Hello World
Foo::baz(); // throws an error

Note how I can call bar() both from the instance variable and statically without getting an error. As long as I don’t reference $this inside of a class function PHP4 will allow me to call it statically whenever I want. This is not necessarily wrong behavior, but since you have no way of enforcing a static nature of some functions things can get nasty. And by nasty I mean messy, and difficult to maintain.

Question is, do I really want to invest time into deploying PHP5 on that server and potentially run into a clusterfuck gauntlet when it inadvertently breaks some code dependencies? Meh… I think I will just have to deal without scope and visibility modifiers…

Related Posts:

  • PHP4 Mail Function: Sent Time is 1 Hour Ahead
  • Ackermann Function in Java
  • JQuery Fun
  • PHP: Export Query Results to a CSV File
  • Metaprogramming in PHP
  • Simple Metaprogramming With Javascript
  • Verizon: .002 Cents
  • Making a Better Use of Script Tags
  • The H-Wing
  • Firefox: Web Page Printing Problems

  • 3 Responses to “PHP4 Static Function Calls”

    1. Gravatar Gemma Peter UNITED KINGDOM Says: Reply to this comment

      I ended up putting this line at the top of any instance methods to emulate the PHP5 differentiation between static and instance methods.

      if(is_null($this)) {die(’instance method cannot be called statically’);}

      Posted using Debian IceWeasel 2.0.0.12 on Debian GNU/Linux Debian GNU/Linux
    2. Gravatar Luke Maciak UNITED STATES Says: Reply to this comment

      Nice! Or, maybe more elegantly:

      if(is_null($this)) throw new Exception('Instance method cannot be called statically.');

      This way gracefully handle errors caused by calling this method statically.

      I’m stealing the idea. )

      Posted using Mozilla Firefox Mozilla Firefox 2.0.0.13 on Ubuntu Linux Ubuntu Linux
    3. Gravatar Luke Maciak UNITED STATES Says: Reply to this comment

      Oh wait… PHP4 doesn’t have exceptions! Damn! (

      LOL

      Posted using Mozilla Firefox Mozilla Firefox 2.0.0.13 on Ubuntu Linux Ubuntu Linux

    Leave a Reply

    XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <pre lang=""> <em> <i> <strike> <strong>

    [Quote selected]