Reputation: 385
I'm wondering why the following lines causes an error. doSomething()
gets called from another PHP file.
class MyClass
{
private $word;
public function __construct()
{
$this->word='snuffy';
}
public function doSomething($email)
{
echo('word:');
echo($this->word); //ERROR: Using $this when not in object context
}
}
Upvotes: 1
Views: 3715
Reputation: 360902
How are you calling the method?
Doing
MyClass::doSomething('[email protected]');
will fail, as it's not a static method, and you're not accessing a static variable.
However, doing
$obj = new MyClass();
$obj->doSomething('[email protected]');
should work.
Upvotes: 4
Reputation: 401182
To use your class and method which are not static
, you must instanciate your class :
$object = new MyClass();
$object->doSomething('[email protected]');
MyClass::doSomething('[email protected]');
Calling this will get you :
Strict standards: Non-static method MyClass::doSomething() should not be called statically
$this
: Fatal error: Using $this when not in object context
Upvotes: 1