Hello,

> You are missing the point in PHP in that case. Because  PHP is dynamic
> scripting language, public properties can be added and removed in the
> object on the fly. That's why there is isset and unset that works on
> object properties. Consider ActiveRecord, DataMappers, ORM, etc. They
> use that 100% to their advantage - I haven't yet seen a model witch
> defines all the properties in PHP code - mostly it takes table columns
> from DB and add these properties in dynamic way.
> That's why it isn't so straight forward of adding properties like you
> propose.

I think you are correct, and I am beginning to see the light on this
issue.  In order for a property to be 100% compatible with regular
variables, it needs to work properly with isset and unset.


> P.S. By the way, maybe I haven't being doing some really crazy stuff
> on PHP, but I rarely define getters and setters in my classes that do
> something except $this->val = $val and return $this->val. Well,
> honestly I haven't worked on some stuff developed by more that 6
> programmers too (Latvia is a small county - no major epic projects at
> all), but still I think my point is valid.

Properties have a variety of uses.  One use is to make a read-only value
that can later have a set method added in a child class.  This is not
possible with the readonly keyword.  You can also provide different
visibilities for the get and set, as well as marking them final or
abstract individually.

You can use properties to provide simple validation.  For example:

class TimePeriod
{
    private $_hours;

    /// Hours must be between 1-12
    public property Hours
    {
        get { return $this->_hours; }
        set
        {
            if ($value < 1 || $value > 12)
            {
                throw new Exception("Hours must be between 1-12");
            }

            $this->_hours = $value;
        }
    }
};

You can also use properties to dynamically generate a value, such as in
one of the examples in the RFC:

class TimePeriod
{
    private $seconds;

    public property Hours
    {
        get { return $this->seconds / 3600; }
    }

    public property Minutes
    {
        get { return $this->seconds / 60; }
    }
};

Here is some additional reading on C# properties from MSDN:

http://msdn.microsoft.com/en-us/library/ms229054.aspx
http://msdn.microsoft.com/en-us/library/ms229006.aspx

- Dennis


--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to