Posted by Rik on 07/29/07 17:08
On Sun, 29 Jul 2007 18:53:24 +0200, Jeff =
<it_consultant1@hotmail.com.NOSPAM> wrote:
> PHP version 5.2.3
> Below you see some .NET code. This is a class contain a private member=
> variable. For other classes to access this private variable they have =
to =
> use
> the public method. I'm wondering if something similar is possible with=
> object-oriented PHP (5.2.3) programming??
>
> class Period
> {
> private double _second;
> public double Seconds
> {
> get { return _second; }
> set { _second =3D value; }
> }
> }
>
>
An implementation of this could be:
<?php
class Period{
private $_second;
=
function __set($name,$value){
echo 'Setting '.$name;
$this->$name =3D $value;
}
function __get($name){
echo 'Getting '.$name;
if(isset($this->$name)) return $this->$name;
echo 'No such property: Period::'.$name;
}
}
$foo =3D new Period();
$foo->_second =3D 123;
echo $foo->_second ;
?>
Check the manual for the __get()/__set() functions. If you want to force=
=
the variable to be a float (or whatever you want) you can enforce that i=
n =
the __set() method.
<http://nl3.php.net/manual/en/language.oop5.overloading.php>
-- =
Rik Wasmus
Navigation:
[Reply to this message]
|