Jump to content

Why are static functions turned into instance menthods?


jordanwb

Recommended Posts

class Foo
{
public static function Bar ()
{
	return "Hello World!";
}
}

$foo = new Foo ();
print $foo->Bar ();

 

Output:

 

Hello World!

 

Why does this happen? Why does PHP turn the static function into an instance function.

Because you can technically call the static method from an instance as long as you don't use any $this stuff.  If you don't want to be able to do that, just do something like:

 

<?php
class Foo
{
private function __construct() { } //no initializations!
public static function Test()
{
	return "Testing worked!";
}
}

Foo::Test(); //works
$foo = new Foo; //errors out!
$foo->Test();  //doesn't even get up to here

In PHP6, I think they decided to throw a fatal error on dynamic calling of a static function.  Maybe it's a warning or something, but I know they decided to make it [like it should be] a no-no.

 

Yup, they fixed it in PHP6 if I remember correctly.

lolwut?

 

The first time I tried to post the forum said that I had already made a post with my IP (or something retarded like that), then I got the same message a second time. Then it said that I had already posted the same message (WTFail?) and I didn't. So I had to wait about a minute before the forum would let me post.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.