note 116344 modified in language.oop5.properties by nikic
| From: | nikic@php.net | Date: | Tue, 27 Sep 2016 14:15:30 +0000 |
| Subject: | note 116344 modified in language.oop5.properties by nikic | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-206686@lists.php.net to get a copy of this message | ||
In case this saves anyone any time, I spent ages working out why the following didn't work:
class MyClass
{
private $foo = FALSE;
public function __construct()
{
$this->$foo = TRUE;
echo($this->$foo);
}
}
$bar = new MyClass();
giving "Fatal error: Cannot access empty property in ...test_class.php on line 8"
The subtle change of removing the $ before accesses of $foo fixes this:
class MyClass
{
private $foo = FALSE;
public function __construct()
{
$this->foo = TRUE;
echo($this->foo);
}
}
$bar = new MyClass();
I guess because it's treating $foo like a variable in the first example, so trying to call
$this->FALSE (or something along those lines) which makes no sense. It's obvious once
you've realised, but there aren't any examples of accessing on this page that show that.
[Editor's note: Removed copy of note by zzzzBov]
--was--
In case this saves anyone any time, I spent ages working out why the following didn't work:
class MyClass
{
private $foo = FALSE;
public function __construct()
{
$this->$foo = TRUE;
echo($this->$foo);
}
}
$bar = new MyClass();
giving "Fatal error: Cannot access empty property in ...test_class.php on line 8"
The subtle change of removing the $ before accesses of $foo fixes this:
class MyClass
{
private $foo = FALSE;
public function __construct()
{
$this->foo = TRUE;
echo($this->foo);
}
}
$bar = new MyClass();
I guess because it's treating $foo like a variable in the first example, so trying to call
$this->FALSE (or something along those lines) which makes no sense. It's obvious once
you've realised, but there aren't any examples of accessing on this page that show that.
&
Do not confuse php's version of properties with properties in other languages (C++ for
example). In php, properties are the same as attributes, simple variables without functionality.
They should be called attributes, not properties.
Properties have implicit accessor and mutator functionality. I've created an abstract class
that allows implicit property functionality.
<?php
abstract class PropertyObject
{
public function __get($name)
{
if (method_exists($this, ($method = 'get_'.$name)))
{
return $this->$method();
}
else return;
}
public function __isset($name)
{
if (method_exists($this, ($method = 'isset_'.$name)))
{
return $this->$method();
}
else return;
}
public function __set($name, $value)
{
if (method_exists($this, ($method = 'set_'.$name)))
{
$this->$method($value);
}
}
public function __unset($name)
{
if (method_exists($this, ($method = 'unset_'.$name)))
{
$this->$method();
}
}
}
?>
after extending this class, you can create accessors and mutators that will be called automagically,
using php's magic methods, when the corresponding property is accessed.
http://php.net/manual/en/language.oop5.properties.php