Hi !
This is my first email here (I'm just a PHP user, with only very basic C
skills, but I'm working on it), and I would love to contribute to this project.
I have been working with Objective-c lately, and it has a very flexible and
short way to deal with properties, which could look like this in PHP :
<?php
class TimePeriod {
protected $seconds;
protected $minutes;
protected $hours;
@synthesize (readwrite) $hours, $minutes;
@synthesize (readonly) $seconds;
}
NB : I intentionally skipped the @property declaration wich I believe is a
specific need for objc (used to allow dot syntax usage).
In objc, this notation allows to :
- use dot syntax for accessing properties (using their getters/setters only,
all properties are protected in objc)
- write your own getters/setters (only the missing ones are generated)
In Php, the pros of this syntax would be :
- very concise
- no interference with existing code (except certain conditions)
- allows to override generated getters and setters with custom code
- may work with the existing code base
The cons :
- new syntax to learn (even if it is quite simple)
- need to use pre-determined setters and getters names to keep simplicity,
which could potentially lead to BC break in some (hopefully rare) cases.
What do you think about it ?
Regards,
Benjamin
Le 29 nov. 2010 à 17:10, Richard Quadling a écrit :
> 2010/11/29 Ángel González <[email protected]>:
>> Richard Quadling wrote:
>> setMilliseconds() should have $value as parameter instead of a magic name.
>>
>> What about allowing this syntax to attach the property to a variable?
>>
>> For instance:
>>
>> <?php
>> class TimePeriod
>> {
>> protected $seconds;
>> protected $minutes;
>> protected $hours;
>>
>> public property Seconds read $seconds write setSeconds;
>> public property Minutes read $seconds write setMinutes;
>> public property Hours read $seconds write setHours;
>>
>> public function setSeconds($seconds)
>> {
>> if ($seconds >= 0 && $seconds < 60) $this->seconds = $seconds;
>> }
>>
>> public function setMinutes($minutes)
>> {
>> if ($minutes >= 0 && $minutes < 60) $this->minutes = $minutes;
>> }
>>
>> public function setHours($hours)
>> {
>> if ($hours >= 0 && $hours < 24) $this->hours = $hours;
>> }
>>
>> }
>>
>> We only want to perform checks on write, so instead of writing the trivial
>> getters, the property is set to the variable itself. Child classes could
>> reattach it to a function if needing more control.
>
> Ouch. I messed up on the example code. Completely forget that there
> was a parameter to process...
>
> As for reading $seconds directly ...
>
> Well.
>
> If you think of the element that follows read as $this->xxxx, then if
> the parser can handle both ...
>
> read $seconds
> read getSeconds
>
> then yes for both.
>
> If not, then I'd guess that the getSeconds version should be the one
> we use as the value may not actually exist without some processing.
>
> Richard.
>
> --
> Richard Quadling
> Twitter : EE : Zend
> @RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php