Skip to content

Instantly share code, notes, and snippets.

@eteubert
Created January 22, 2012 13:14
Show Gist options
  • Select an option

  • Save eteubert/1657038 to your computer and use it in GitHub Desktop.

Select an option

Save eteubert/1657038 to your computer and use it in GitHub Desktop.
PHP Weirdness: Calling Non-Static Methods Statically
<?php
class A {
public function foo() {
echo "Hello World";
}
}
A::foo(); // => Hello World
<?php
class A {
public $bar = 42;
public function foo() {
echo $this->bar;
}
}
A::foo(); // => Fatal error: Using $this when not in object context in /code/Yk6d7i on line 5
<?php
class A {
public $bar = 42;
public function foo() {
echo $this->bar;
}
}
class B {
public $bar = 23;
public function __construct() {
A::foo();
}
}
new B; // => 23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment