I have been doing some research over time and wanted to know if using properties in a PHP class can be done like C#, I have found these questions which game some good answers and pointers, but didn t help me really:
-
http://stackoverflow.com/questions/3788538/should-i-use-c-like-property-handling-in-php
http://stackoverflow.com/questions/3788538/should-i-use-c-like-property-handling-in-php
http://stackoverflow.com/questions/4925317/get-and-set-private-property-in-php-as-in-c-sharp-without-using-getter-setter
http://stackoverflow.com/questions/4925317/get-and-set-private-property-in-php-as-in-c-sharp-without-using-getter-setter
Although, as I said, these give good answers, I still can t make up my mind on these, for example, I want to be able to do something such as:
class Foo { public $bar { get { return $this->bar; } set { $this->bar = value; } } }
But I am unable to!
What is the best way to do this within the constraints of PHP?
The kind of thing I have been able to come up with is this:
class user { private static $link; private static $init; private function __construct () { global $link; static::$link = $link; } public static function __INIT__ () { return static::$init = (null === static::$init ? new self() : static::$init); } public static $username; public function username( $value = ) { if ( $value == || empty ( $value ) || !$value ) return static::$username; else static::$username = $value; } }