# Real properties in PHP

- Thread: https://externals.io/message/18407
- Original archive: https://news-web.php.net/php.internals/18407
- Started by Marian Kostadinov on 2005-08-25T10:29:20+00:00
- Messages: 5
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #18407 — Marian Kostadinov — 2005-08-25T10:29:20+00:00

URL: https://externals.io/message/18407#18407

I wrote about real properies a few days ago but no one seems to be
interested in it.

I think that it would be nice to have real properties present in PHP. I
mean properties like those in C# but a little more flexible by allowing
getter and setter to have different visibility. See sample code below:

Sample code:
---------------
class person {
  public $firstName, $lastName;

  public getter fullName { // may also have ()
   return "{$this->firstName} {$this->lastName}";
  }

  public setter fullName ($value) { // may also not have ($value) but
a special var.
   list ($this->firstName, $this->lastName) = explode (' ', $value, 2);
  }
}

 $p = new person;
 $p->fullName = "Foo bar";
 echo $p->firstName . "\n";
 echo $p->lastName . "\n";
 echo $p->fullName . "\n";

Sample result:
----------------
Foo
bar
Foo bar

---

## #18408 — Christian Schneider — 2005-08-25T10:37:48+00:00

URL: https://externals.io/message/18407#18408 · In reply to: #18407

Marian Kostadinov wrote:
>   public getter fullName { // may also have ()
>    return "{$this->firstName} {$this->lastName}";
>   }
> 
>   public setter fullName ($value) { // may also not have ($value) but
> a special var.
>    list ($this->firstName, $this->lastName) = explode (' ', $value, 2);
>   }

This can be easily done with

function __get($name)
{
         $get = "get_$name";

         return $this->$get();
}

function __set($name, $value)
{
         $set = "set_$name";

         $this->$set($value);
}

in your base class (the function names are then get_fullName/set_fullName.

- Chris

---

## #18409 — Marian Kostadinov — 2005-08-25T10:51:50+00:00

URL: https://externals.io/message/18407#18409 · In reply to: #18408

> Marian Kostadinov wrote:
> > […]
> 
> This can be easily done with
> 
> function __get($name)
> {
>          $get = "get_$name";
> 
>          return $this->$get();
> }
> 
> function __set($name, $value)
> {
>          $set = "set_$name";
> 
>          $this->$set($value);
> }
> 
> in your base class (the function names are then get_fullName/set_fullName.
> 
> - Chris
> 

I know this solutions but it does not solve the property visibility
issues and good overloading rules like in common methods. Let's say
you have about 6-7 suche proterties which you expect to overload and
some of the are private e.t.c

---

## #18410 — Derick Rethans — 2005-08-25T10:53:31+00:00

URL: https://externals.io/message/18407#18410 · In reply to: #18408

> This can be easily done with
> 
> function __get($name)
> {
>         $get = "get_$name";
> 
>         return $this->$get();
> }
> 
> function __set($name, $value)
> {
>         $set = "set_$name";
> 
>         $this->$set($value);
> }
> 
> in your base class (the function names are then get_fullName/set_fullName.

But that has plenty of problems, like I described here:
http://news.php.net/php.internals/17491

Derick
-- 
Derick Rethans
http://derickrethans.nl | http://ez.no | http://xdebug.org

---

## #18609 — Ron Korving — 2005-08-31T14:50:30+00:00

URL: https://externals.io/message/18407#18609 · In reply to: #18407

I very much like your idea and syntax. I come from a Delphi background (when
it comes to properties) and I must say I like your solution. It's easy and
effective. Yes, it can be done with __set() and __get(), but it sure is
uglier when you have a wide and constant range of properties.

+1 (with 0 karma anyway).

Ron


"Marian Kostadinov" <manchokapitancho@gmail.com> wrote in message
news:bec76cf9050825032978cd308d@mail.gmail.com...
I wrote about real properies a few days ago but no one seems to be
interested in it.

I think that it would be nice to have real properties present in PHP. I
mean properties like those in C# but a little more flexible by allowing
getter and setter to have different visibility. See sample code below:

Sample code:
---------------
class person {
  public $firstName, $lastName;

  public getter fullName { // may also have ()
   return "{$this->firstName} {$this->lastName}";
  }

  public setter fullName ($value) { // may also not have ($value) but
a special var.
   list ($this->firstName, $this->lastName) = explode (' ', $value, 2);
  }
}

 $p = new person;
 $p->fullName = "Foo bar";
 echo $p->firstName . "\n";
 echo $p->lastName . "\n";
 echo $p->fullName . "\n";

Sample result:
----------------
Foo
bar
Foo bar
