Hello internals,
__get() and __set() are great, but 90% of the time, I find myself using them to create public readonly properties.
The only problem with this is it is horridly inefficient, consuming at least 1 function call and one switch statement (or equiv) per property read.
Would it be possible to create a new object property attribute: readonly
class xx
{
readonly $bar;
}
$o = new xx();
$o->bar = 10;
FATAL ERROR
This way, PHP would allow reading (as if it were public), but only allow writing from within the class.
I think it could really boost performance of complicated application logic that wishes to enforce good visibility.
Comments?
PS. What brought this up was some serious performance issues in a piece of code that I am working with - most of which can be tied back to __get() performance.
--
Best regards,
Jason Garber mailto:jason@ionzoft.com IonZoft, Inc.
In C++ you'd use private for this. All object members are readable, but modifyability depends on the relation between the caller and the object. I don't quite understand why PHP is doing it differently.
- Ron
"Jason Garber" jason@ionzoft.com schreef in bericht news:785810036.20060511193536@ionzoft.com...
Quoted text · 18 lines
Hello internals,
__get() and __set() are great, but 90% of the time, I find myself using them to create public readonly properties.
The only problem with this is it is horridly inefficient, consuming at least 1 function call and one switch statement (or equiv) per property read.
Would it be possible to create a new object property attribute: readonly
class xx
{
readonly $bar;
}$o = new xx();
$o->bar = 10;
FATAL ERROR
This way, PHP would allow reading (as if it were public), but only allow writing from within the class.
I think it could really boost performance of complicated application logic that wishes to enforce good visibility.
Comments?
PS. What brought this up was some serious performance issues in a piece of code that I am working with - most of which can be tied back to __get() performance.
--
Best regards,
Jason Garber mailto:jason@ionzoft.com IonZoft, Inc.
Ron Korving wrote:
In C++ you'd use private for this. All object members are readable, ...
Could you elaborate on this?
--
Hartmut Holzgraefe, Senior Support Engineer . MySQL AB, www.mysql.com
Are you certified? http://www.mysql.com/training/certification
My bad, you are absolutely right, I must be confused with another language, but I wonder which one.... odd.
Ron
"Hartmut Holzgraefe" hartmut@php.net wrote in message news:4465FBED.8070205@php.net...
Quoted text · 6 lines
Ron Korving wrote:
In C++ you'd use private for this. All object members are readable, ...
Could you elaborate on this?
--
Hartmut Holzgraefe, Senior Support Engineer . MySQL AB, www.mysql.comAre you certified? http://www.mysql.com/training/certification
Hello Ron,
hehe, maybe confused with delphi or borlands c++ additons? Speaking of which before we add 'readonly' we should go for full property support but on the other hand that might be a little bit too much until php is used with code generators and gui designers where code inspectors execute and manipulate source/code while developing. Ups wrong tool....
regards
marcus
Saturday, May 13, 2006, 8:52:39 PM, you wrote:
My bad, you are absolutely right, I must be confused with another language, but I wonder which one.... odd.
Ron
Quoted text · 7 lines
"Hartmut Holzgraefe" hartmut@php.net wrote in message news:4465FBED.8070205@php.net...
Ron Korving wrote:
In C++ you'd use private for this. All object members are readable, ...
Could you elaborate on this?
--
Hartmut Holzgraefe, Senior Support Engineer . MySQL AB, www.mysql.comAre you certified? http://www.mysql.com/training/certification
Best regards,
Marcus
Quoted text · 4 lines
hehe, maybe confused with delphi or borlands c++ additons? Speaking of
which before we add 'readonly' we should go for full property support but
on the other hand that might be a little bit too much until php is used with code generators and gui designers where code inspectors execute and
manipulate source/code while developing.
Hi Marcus,
Full property support is high on my wishlist for 6.0. I was a Delphi programmer for 5 years and miss properties. C#, Ruby, and Java all have built in property support with accessor methods, or at least a single standard implementation that all the tools can get behind.
__get and __set leave you in complete limbo for both source code and reflection based tools. I think the language support has to come before the tools.
I think this can be implemented by adding a getter and setter field to zend_property_info, then checking for it in zend_std_read_property, etc. Although, I'm sure there's more to it than that. Such an implementation would probably be 3 to 4 times faster than __get(). No switch, no $name parameter, perhaps no guard logic. Checking for a getter or setter in zend_property_info would be a fast boolean test on a data structure thats already available, so I believe there would be little overhead.
Here are a few use cases and syntax suggestions...
A. Declaring a property with accessor methods:
public $foo read getFoo write setFoo;
B. Read only property with accessor method could be declared:
public $foo read getFoo;
C. A shortcut notation could automatically generate the accessor method based on another property:
public $foo read $_foo;
// internally generated method ala C# property implementation: // public function __get_foo() { return $this->$_foo; }
D. Similar to read only, you could have split visibility, for example, a public getter and a protected setter:
public $foo read getFoo write protected setFoo; // Handy use case, not crazy about this syntax
public function getFoo() { return $this->_foo; }
protected function setFoo($value) { $this->_foo = $value }
E. To avoid warnings, declare the internal storage, too:
public $foo read $_foo write setFoo, protected $_foo;
public $foo read $_foo, protected $_foo; // readonly
F. Properties with accessor methods cannot be directly initialized.
Their internal storage can, however:
public $foo read $_foo write setFoo, protected $_foo = 'bar';
G. calling unset() on a property with accessor methods could call the setter with NULL.
H. calling isset() on a property with accessor methods returns FALSE if the property does not exist, otherwise calls the getter and compares against NULL for compatibility purposes. (?)
I. calling property_exists() on a property with accessor methods would always return TRUE.
J. The setter and getter could be inspected via ReflectionProperty.
K. Unlike __get, subclass property definitions could override the parent declarations:
class Foo { public $prop; }
class Bar extends Foo { public $prop read getProp write setProp; ... }
L. An abstract class need not declare the actual accessor methods, they could be added as abstract by default:
abstract class Bar { public $foo read getFoo write setFoo; }
Just a few thoughts. I am sure there are other possibilities for the same use cases.
Best Regards,
Jeff
Likely Delphi yeah :)
Ron
"Marcus Boerger" helly@php.net schreef in bericht news:1771237045.20060514011813@marcus-boerger.de...
Quoted text · 17 lines
Hello Ron,
hehe, maybe confused with delphi or borlands c++ additons? Speaking of which before we add 'readonly' we should go for full property support but on the other hand that might be a little bit too much until php is used with code generators and gui designers where code inspectors execute and manipulate source/code while developing. Ups wrong tool....
regards
marcusSaturday, May 13, 2006, 8:52:39 PM, you wrote:
My bad, you are absolutely right, I must be confused with another language,
but I wonder which one.... odd.Ron
"Hartmut Holzgraefe" hartmut@php.net wrote in message news:4465FBED.8070205@php.net...
Ron Korving wrote:
In C++ you'd use private for this. All object members are readable, ...
Could you elaborate on this?
--
Hartmut Holzgraefe, Senior Support Engineer . MySQL AB, www.mysql.comAre you certified? http://www.mysql.com/training/certification
Best regards,
Marcus
I can see where it could come in handy but I honestly think it'd be bloat. We have to relax with the OO features because the increased code size has already made it harder to maintain and it has the potential to make PHP far more complicated than what it should be.
At 04:35 PM 5/11/2006, Jason Garber wrote:
Quoted text · 18 lines
Hello internals,
__get() and __set() are great, but 90% of the time, I find myself using them to create public readonly properties.
The only problem with this is it is horridly inefficient, consuming at least 1 function call and one switch statement (or equiv) per property read.
Would it be possible to create a new object property attribute: readonly
class xx
{
readonly $bar;
}$o = new xx();
$o->bar = 10;
FATAL ERROR
This way, PHP would allow reading (as if it were public), but only allow writing from within the class.
I think it could really boost performance of complicated application logic that wishes to enforce good visibility.
Comments?
PS. What brought this up was some serious performance issues in a piece of code that I am working with - most of which can be tied back to __get() performance.
--
Best regards,
Jason Garber mailto:jason@ionzoft.com IonZoft, Inc.
__get() and __set() are great, but 90% of the time, I find myself using them to create public readonly properties.
I can see where it could come in handy but I honestly think it'd be bloat. We have to relax with the OO features because the increased code size has already made it harder to maintain and it has the potential to make PHP far more complicated than what it should be.
An extension could accomplish this by exporting an interface which overrides the object's read_property() method. One could "flag" which properties are allowed to be accessed r/o by giving them a distinct name e.g.:
class foo implements ReadOnlyProperties {
private $__ro__bar;
function __construct($val) {
$this->__ro__bar = $val;
}
}
$f = new foo(123);
var_dump($f->bar);
The special getter sees that bar isn't in the properties table and looks for __ro__bar instead (overriding visibility restrictions in the process). Since there's no write_property override, the prop remains unwritable from outside of the object.
-Sara
Hello Sara,
while all what you wrote is doable i'd suggest a slightly different approach. If the name of the variable is prefixed with "r_" and written from outside the class an error will be issued.
There's only one thing to be considered. The proposal cannot work for overloaded internal objects like those in ext/dom for example.
Friday, May 12, 2006, 6:14:20 PM, you wrote:
__get() and __set() are great, but 90% of the time, I find myself using them to create public readonly properties.
I can see where it could come in handy but I honestly think it'd be bloat. We have to relax with the OO features because the increased code size has already made it harder to maintain and it has the potential to make PHP far more complicated than what it should be.
An extension could accomplish this by exporting an interface which overrides the object's read_property() method. One could "flag" which properties are allowed to be accessed r/o by giving them a distinct name e.g.:
Quoted text · 6 lines
class foo implements ReadOnlyProperties {
private $__ro__bar;
function __construct($val) {
$this->__ro__bar = $val;
}
}
$f = new foo(123);
var_dump($f->bar);
The special getter sees that bar isn't in the properties table and looks for __ro__bar instead (overriding visibility restrictions in the process). Since there's no write_property override, the prop remains unwritable from outside of the object.
-Sara
Best regards,
Marcus
I agree with Andi on that (surprise surprise :)
What does that give you that class constants don't?
Zeev
At 18:34 12/05/2006, Andi Gutmans wrote:
Quoted text · 20 lines
I can see where it could come in handy but I honestly think it'd be bloat. We have to relax with the OO features because the increased code size has already made it harder to maintain and it has the potential to make PHP far more complicated than what it should be.
At 04:35 PM 5/11/2006, Jason Garber wrote:
Hello internals,
__get() and __set() are great, but 90% of the time, I find myself using them to create public readonly properties.
The only problem with this is it is horridly inefficient, consuming at least 1 function call and one switch statement (or equiv) per property read.
Would it be possible to create a new object property attribute: readonly
class xx
{
readonly $bar;
}$o = new xx();
$o->bar = 10;
FATAL ERROR
This way, PHP would allow reading (as if it were public), but only allow writing from within the class.
I think it could really boost performance of complicated application logic that wishes to enforce good visibility.
Comments?
PS. What brought this up was some serious performance issues in a piece of code that I am working with - most of which can be tied back to __get() performance.
--
Best regards,
Jason Garber mailto:jason@ionzoft.com IonZoft, Inc.
Strike that, I was smoking something strong :) Class constants are not really relevant for this use case.
At 20:57 15/05/2006, Zeev Suraski wrote:
Quoted text · 24 lines
I agree with Andi on that (surprise surprise :)
What does that give you that class constants don't?
Zeev
At 18:34 12/05/2006, Andi Gutmans wrote:
I can see where it could come in handy but I honestly think it'd be bloat. We have to relax with the OO features because the increased code size has already made it harder to maintain and it has the potential to make PHP far more complicated than what it should be.
At 04:35 PM 5/11/2006, Jason Garber wrote:
Hello internals,
__get() and __set() are great, but 90% of the time, I find myself using them to create public readonly properties.
The only problem with this is it is horridly inefficient, consuming at least 1 function call and one switch statement (or equiv) per property read.
Would it be possible to create a new object property attribute: readonly
class xx
{
readonly $bar;
}$o = new xx();
$o->bar = 10;
FATAL ERROR
This way, PHP would allow reading (as if it were public), but only allow writing from within the class.
I think it could really boost performance of complicated application logic that wishes to enforce good visibility.
Comments?
PS. What brought this up was some serious performance issues in a piece of code that I am working with - most of which can be tied back to __get() performance.
--
Best regards,
Jason Garber mailto:jason@ionzoft.com IonZoft, Inc.
Zeev Suraski wrote:
What does that give you that class constants don't?
i on the other hand fail to see how it is related to class constants at all?
--
Hartmut Holzgraefe, Senior Support Engineer . MySQL AB, www.mysql.com
Are you certified? http://www.mysql.com/training/certification
Hartmut Holzgraefe wrote:
Zeev Suraski wrote:
What does that give you that class constants don't?
i on the other hand fail to see how it is related to class constants at all?
hm ... this was not supposed to get out as i had seen your reply to yourself ...
--
Hartmut Holzgraefe, Senior Support Engineer . MySQL AB, www.mysql.com
Are you certified? http://www.mysql.com/training/certification
Jason Garber wrote:
Quoted text · 9 lines
Would it be possible to create a new object property attribute: readonly
class xx
{
readonly $bar;
}$o = new xx();
$o->bar = 10;
FATAL ERROR
This way, PHP would allow reading (as if it were public), but only allow writing from within the class.
Uhh... how about using private and only using a "regular" getter (the Java-style) and no setter?
class xx
{
private $bar;
public getBar() { return $bar; }
}
$o = new xx();
$o->bar = 10; // already emits FATAL ERROR
Bastian Grupe wrote:
Quoted text · 6 lines
Uhh... how about using private and only using a "regular" getter (the Java-style) and no setter?
class xx
{
private $bar;public getBar() { return $bar; }}
i think you're missing the point, the idea is to not have to write that extra getter line ... less typing and less error prone ... and easier to read, too IMHO
You didn't want to return $bar in your example, you wanted to return $this->bar so you already ran into one of the issues here ;)
--
Hartmut Holzgraefe, Senior Support Engineer . MySQL AB, www.mysql.com
Are you certified? http://www.mysql.com/training/certification
Hartmut Holzgraefe wrote:
Bastian Grupe wrote:
You didn't want to return $bar in your example, you wanted to return $this->bar so you already ran into one of the issues here ;)
Blame my recent use of Java here ;-)
Well, I think the whole point of ppp is to having to use setters and getters consistently.
I personally wouldn't like to be able to access some members which are private, and not others. It just feels wrong.
And I don't know whether or not less typing is really a good argument in this situation (think unreadable code in shortcut-ish programming languages).
-- Bastian
Bastian Grupe wrote:
Blame my recent use of Java here ;-)
Well, I think the whole point of ppp is to having to use setters and getters consistently.
i'm going to blame your use of Java for this one, ppp is way older than the setter/getter fashion and as far as i remember the main reason to introduce the setter/getter pattern into java was to have a standard way for Java IDEs to provide access to Java Bean properties in property dialogs in their GUI design tools
I personally wouldn't like to be able to access some members which are private, and not others. It just feels wrong.
Think of it as a more fine grained permission system, like unix file attributes. Reading and writing a property value are two different operations and it makes sense to distinguish access rights not only by ownership but also by type of operation.
And I don't know whether or not less typing is really a good argument in this situation (think unreadable code in shortcut-ish programming languages).
Less typing is not an argument by itself, else we'd all do APL
But less typing is less error prone (and no, plese don't mention auto-completion here ;), it can be less readable, too, and in this special case it spreads information that should be in one place. Maintainability can become an issue, too.
Take a look at typical PHP class implementations: they have all properties on top followed by the member functions. So to find out whether a private property is really provite or whether it has a getter or even a setter, too, i would have to browse the full class code.
class example {
private $foo;
private $bar;
[... more properties ...]
function __construct() {...}
function __destruct() {...}
function getFoo() {...}
[... more code ...]
}
So $foo is readonly here and $bar is really private. Or wait, maybe we have just overlooked getBar()?
With
readonly $foo;
on the other hand you have all the information in one place.
If you want to go the getter/setter path all the way then we wouldn't need all the ppp stuff anymore alltogether, we would just make everything private and have the getter and setter decide (using instanceof on $this etc.) the access rights.
--
Hartmut Holzgraefe, Senior Support Engineer . MySQL AB, www.mysql.com
Are you certified? http://www.mysql.com/training/certification
It seems to me this would be a great option to add. How difficult would it be? Would it take significant editing of the source code? I don't see the issue in adding it - seems like it would have plenty of places to be used. Though, if it is added, the name 'readonly' seems a little misleading. It gives off the idea of being able to set it, and not edit again, and not only being able to edit it inside the class.
Quoted text · 25 lines
Bastian Grupe wrote:
Blame my recent use of Java here ;-)
Well, I think the whole point of ppp is to having to use setters and getters consistently.
i'm going to blame your use of Java for this one, ppp is way older than the setter/getter fashion and as far as i remember the main reason to introduce the setter/getter pattern into java was to have a standard way for Java IDEs to provide access to Java Bean properties in property dialogs in their GUI design tools
I personally wouldn't like to be able to access some members which are private, and not others. It just feels wrong.
Think of it as a more fine grained permission system, like unix file attributes. Reading and writing a property value are two different operations and it makes sense to distinguish access rights not only by ownership but also by type of operation.
And I don't know whether or not less typing is really a good argument in this situation (think unreadable code in shortcut-ish programming languages).
Less typing is not an argument by itself, else we'd all do APL
But less typing is less error prone (and no, plese don't mention auto-completion here ;), it can be less readable, too, and in this special case it spreads information that should be in one place. Maintainability can become an issue, too.
Take a look at typical PHP class implementations: they have all properties on top followed by the member functions. So to find out whether a private property is really provite or whether it has a getter or even a setter, too, i would have to browse the full class code.
class example {
private $foo;
private $bar;
[... more properties ...]function __construct() {...} function __destruct() {...} function getFoo() {...} [... more code ...]}
So $foo is readonly here and $bar is really private. Or wait, maybe we have just overlooked getBar()?
With
readonly $foo;
on the other hand you have all the information in one place.
If you want to go the getter/setter path all the way then we wouldn't need all the ppp stuff anymore alltogether, we would just make everything private and have the getter and setter decide (using instanceof on $this etc.) the access rights.
--
Hartmut Holzgraefe, Senior Support Engineer . MySQL AB, www.mysql.comAre you certified? http://www.mysql.com/training/certification
--
--
Graham Christensen
www.itrebal.com
www.iamgraham.net
Hello,
PHP implements many features, and skips many features. I think the rule of thumb needs to be that if a feature is implemented, finish it.
For example, if you provide __get() and __set(), provide an efficient way of handling the normal use case.
If you start triggering an E_NOTICE when an undefined variable is accessed, then provide an easy way to access variables that may or may not be set.
If you provide a __tostring() method, then finish it so that is gets called when an object is cast to a string, concatenated with a string, as well as being output with echo, print.
There are a lot of casual users of PHP. There are also the people out there who are buying the Zend products, buying the MySQL support contracts, using PHP at Yahoo! -- the people who have chosen to use PHP OVER Java/.NET/Perl, because it is a great language -- the people who need the completed features because they are running multi-million-dollar businesses on this platform.
Take a step back and truly evaluate why someone in a demanding situation might want every bit of performance and readability that they can squeeze out of existing language features.
I'm not talking about adding hundreds of new features, or turning PHP into the next java. It's about real business cases.
It's not about what YOU WOULD NOT use, it's about what a lot of people need.
--
Best regards,
Jason Garber mailto:jason@ionzoft.com IonZoft, Inc.
Friday, May 12, 2006, 3:16:27 PM, you wrote:
igc> It seems to me this would be a great option to add. How difficult would it igc> be? Would it take significant editing of the source code? I don't see the igc> issue in adding it - seems like it would have plenty of places to be used. igc> Though, if it is added, the name 'readonly' seems a little misleading. It igc> gives off the idea of being able to set it, and not edit again, and not only igc> being able to edit it inside the class.
igc> On 5/12/06, Hartmut Holzgraefe hartmut@php.net wrote:
Quoted text · 24 lines
Bastian Grupe wrote:
Blame my recent use of Java here ;-)
Well, I think the whole point of ppp is to having to use setters and getters consistently.
i'm going to blame your use of Java for this one, ppp is way older than the setter/getter fashion and as far as i remember the main reason to introduce the setter/getter pattern into java was to have a standard way for Java IDEs to provide access to Java Bean properties in property dialogs in their GUI design tools
I personally wouldn't like to be able to access some members which are private, and not others. It just feels wrong.
Think of it as a more fine grained permission system, like unix file attributes. Reading and writing a property value are two different operations and it makes sense to distinguish access rights not only by ownership but also by type of operation.
And I don't know whether or not less typing is really a good argument in this situation (think unreadable code in shortcut-ish programming languages).
Less typing is not an argument by itself, else we'd all do APL
But less typing is less error prone (and no, plese don't mention auto-completion here ;), it can be less readable, too, and in this special case it spreads information that should be in one place. Maintainability can become an issue, too.
Take a look at typical PHP class implementations: they have all properties on top followed by the member functions. So to find out whether a private property is really provite or whether it has a getter or even a setter, too, i would have to browse the full class code.
class example {
private $foo;
private $bar;
[... more properties ...]function __construct() {...} function __destruct() {...} function getFoo() {...} [... more code ...]}
So $foo is readonly here and $bar is really private. Or wait, maybe we have just overlooked getBar()?
With
readonly $foo;
on the other hand you have all the information in one place.
If you want to go the getter/setter path all the way then we wouldn't need all the ppp stuff anymore alltogether, we would just make everything private and have the getter and setter decide (using instanceof on $this etc.) the access rights.
--
Hartmut Holzgraefe, Senior Support Engineer . MySQL AB, www.mysql.comAre you certified? http://www.mysql.com/training/certification
I can take any feature in PHP and add features :)
At 01:44 PM 5/12/2006, Jason Garber wrote:
Quoted text · 42 lines
Hello,
PHP implements many features, and skips many features. I think the rule of thumb needs to be that if a feature is implemented, finish it.
For example, if you provide __get() and __set(), provide an efficient way of handling the normal use case.
If you start triggering an
E_NOTICEwhen an undefined variable is accessed, then provide an easy way to access variables that may or may not be set.If you provide a __tostring() method, then finish it so that is gets called when an object is cast to a string, concatenated with a string, as well as being output with echo, print.
There are a lot of casual users of PHP. There are also the people out there who are buying the Zend products, buying the MySQL support contracts, using PHP at Yahoo! -- the people who have chosen to use PHP OVER Java/.NET/Perl, because it is a great language -- the people who need the completed features because they are running multi-million-dollar businesses on this platform.
Take a step back and truly evaluate why someone in a demanding situation might want every bit of performance and readability that they can squeeze out of existing language features.
I'm not talking about adding hundreds of new features, or turning PHP into the next java. It's about real business cases.
It's not about what YOU WOULD NOT use, it's about what a lot of people need.
--
Best regards,
Jason Garber mailto:jason@ionzoft.com IonZoft, Inc.Friday, May 12, 2006, 3:16:27 PM, you wrote:
igc> It seems to me this would be a great option to add. How difficult would it
igc> be? Would it take significant editing of the source code? I don't see the igc> issue in adding it - seems like it would have plenty of places to be used.
igc> Though, if it is added, the name 'readonly' seems a little misleading. It igc> gives off the idea of being able to set it, and not edit again, and not only
igc> being able to edit it inside the class.igc> On 5/12/06, Hartmut Holzgraefe hartmut@php.net wrote:
Bastian Grupe wrote:
Blame my recent use of Java here ;-)
Well, I think the whole point of ppp is to having to use setters and getters consistently.
i'm going to blame your use of Java for this one, ppp is way older than the setter/getter fashion and as far as i remember the main reason to introduce the setter/getter pattern into java was to have a standard way for Java IDEs to provide access to Java Bean properties in property dialogs in their GUI design tools
I personally wouldn't like to be able to access some members which are private, and not others. It just feels wrong.
Think of it as a more fine grained permission system, like unix file attributes. Reading and writing a property value are two different operations and it makes sense to distinguish access rights not only by ownership but also by type of operation.
And I don't know whether or not less typing is really a good argument in this situation (think unreadable code in shortcut-ish programming languages).
Less typing is not an argument by itself, else we'd all do APL
But less typing is less error prone (and no, plese don't mention auto-completion here ;), it can be less readable, too, and in this special case it spreads information that should be in one place. Maintainability can become an issue, too.
Take a look at typical PHP class implementations: they have all properties on top followed by the member functions. So to find out whether a private property is really provite or whether it has a getter or even a setter, too, i would have to browse the full class code.
class example {
private $foo;
private $bar;
[... more properties ...]function __construct() {...} function __destruct() {...} function getFoo() {...} [... more code ...]}
So $foo is readonly here and $bar is really private. Or wait, maybe we have just overlooked getBar()?
With
readonly $foo;
on the other hand you have all the information in one place.
If you want to go the getter/setter path all the way then we wouldn't need all the ppp stuff anymore alltogether, we would just make everything private and have the getter and setter decide (using instanceof on $this etc.) the access rights.
--
Hartmut Holzgraefe, Senior Support Engineer . MySQL AB, www.mysql.comAre you certified? http://www.mysql.com/training/certification
Andi Gutmans wrote:
I can take any feature in PHP and add features :)
Is that an offer ;-)? I've got a couple you can add, lol.
Dante
Quoted text · 50 lines
At 01:44 PM 5/12/2006, Jason Garber wrote:
Hello,
PHP implements many features, and skips many features. I think the rule of thumb needs to be that if a feature is implemented, finish it.
For example, if you provide __get() and __set(), provide an efficient way of handling the normal use case.
If you start triggering an
E_NOTICEwhen an undefined variable is accessed, then provide an easy way to access variables that may or may not be set.If you provide a __tostring() method, then finish it so that is gets called when an object is cast to a string, concatenated with a string, as well as being output with echo, print.
There are a lot of casual users of PHP. There are also the people out there who are buying the Zend products, buying the MySQL support contracts, using PHP at Yahoo! -- the people who have chosen to use PHP OVER Java/.NET/Perl, because it is a great language -- the people who need the completed features because they are running multi-million-dollar businesses on this platform.
Take a step back and truly evaluate why someone in a demanding situation might want every bit of performance and readability that they can squeeze out of existing language features.
I'm not talking about adding hundreds of new features, or turning PHP into the next java. It's about real business cases.
It's not about what YOU WOULD NOT use, it's about what a lot of people need.
--
Best regards,
Jason Garber mailto:jason@ionzoft.com IonZoft, Inc.Friday, May 12, 2006, 3:16:27 PM, you wrote:
igc> It seems to me this would be a great option to add. How difficult would it
igc> be? Would it take significant editing of the source code? I don't see the
igc> issue in adding it - seems like it would have plenty of places to be used.
igc> Though, if it is added, the name 'readonly' seems a little misleading. It
igc> gives off the idea of being able to set it, and not edit again, and not only
igc> being able to edit it inside the class.igc> On 5/12/06, Hartmut Holzgraefe hartmut@php.net wrote:
Bastian Grupe wrote:
Blame my recent use of Java here ;-)
Well, I think the whole point of ppp is to having to use setters and
getters consistently.i'm going to blame your use of Java for this one, ppp is way older than the setter/getter fashion and as far as i remember the main reason to introduce the setter/getter pattern into java was to have a standard way for Java IDEs to provide access to Java Bean properties in property dialogs in their GUI design tools
I personally wouldn't like to be able to access some members which are
private, and not others. It just feels wrong.Think of it as a more fine grained permission system, like unix file attributes. Reading and writing a property value are two different operations and it makes sense to distinguish access rights not only by ownership but also by type of operation.
And I don't know whether or not less typing is really a good argument in
this situation (think unreadable code in shortcut-ish programming languages).Less typing is not an argument by itself, else we'd all do APL
But less typing is less error prone (and no, plese don't mention auto-completion here ;), it can be less readable, too, and in this special case it spreads information that should be in one place. Maintainability can become an issue, too.
Take a look at typical PHP class implementations: they have all properties on top followed by the member functions. So to find out whether a private property is really provite or whether it has a getter or even a setter, too, i would have to browse the full class code.
class example {
private $foo;
private $bar;
[... more properties ...]function __construct() {...} function __destruct() {...} function getFoo() {...} [... more code ...]}
So $foo is readonly here and $bar is really private. Or wait, maybe we have just overlooked getBar()?
With
readonly $foo;
on the other hand you have all the information in one place.
If you want to go the getter/setter path all the way then we wouldn't need all the ppp stuff anymore alltogether, we would just make everything private and have the getter and setter decide (using instanceof on $this etc.) the access rights.
--
Hartmut Holzgraefe, Senior Support
Engineer .
MySQL AB, www.mysql.comAre you certified? http://www.mysql.com/training/certification
Jason Garber wrote:
There are a lot of casual users of PHP. There are also the people out there who are buying the Zend products, buying the MySQL support contracts, using PHP at Yahoo! -- the people who have chosen to use PHP OVER Java/.NET/Perl, because it is a great language -- the people who need the completed features because they are running multi-million-dollar businesses on this platform.
Multi-billion in the case of Yahoo! and we can speak for ourselves. The things you listed are irrelevant to us. We have much bigger fish to fry and we are doing so.
-Rasmus
Definatly would love to see readonly, It would remove 1000's of lines of useless code from 100's of projects - which might make a few more public PHP projects readable ;)
It replaces pointless giberish with something explicit clean and elegant..
Looks alot tidier than getters, or __get kludges.
Regards
Alan
Hartmut Holzgraefe wrote:
Quoted text · 9 lines
Bastian Grupe wrote:
Uhh... how about using private and only using a "regular" getter (the Java-style) and no setter?
class xx
{
private $bar;public getBar() { return $bar; }}
i think you're missing the point, the idea is to not have to write that extra getter line ... less typing and less error prone ... and easier to read, too IMHO
You didn't want to return $bar in your example, you wanted to return $this->bar so you already ran into one of the issues here ;)
Jason Garber wrote:
Would it be possible to create a new object property attribute: readonly
Why not make it a little more generic? Say, make readonly an attribute that applies to the ppp tags as opposed to making it a new access class.
Eg. if a member is 'public readonly' then it's read/write for private and protected, but read-only in a public context. Similarly, 'protected readonly' is read/write in a private context, read-only in protected and not visible in public. That would make 'private readonly' read-only for private methods, and not visible to anything else, which is probably only useful for properties generated by extensions.
Or is this what you're proposing and I'm just being dense?
I have to say that in second thought (after realizing what you really meant) it sounds like a very useful feature.
The main thing that worries me is the complexity to the end user, which is already in a pretty bad shape as it is today, and many people here care very little about it, because they can't relate to beginners and average developers. Private/protected/public is a challenge to many of them (not the theory, real world situations), doubling complexity with a modifier is not a good idea.
In order to push complexity down I'd avoid making this yet another modifier, and in fact make this an access level, on par with private/protected/public, that would behave like public, except for when outside the object scope (i.e., have it between protected and public). One of the trickey things would be finding an acceptable name, since 'readonly' implies something which this variable isn't (it's very much writable, from the right places). Maybe something like 'visible' (of course preferably we need to find something that begins with 'p'...)
Zeev
At 02:35 12/05/2006, Jason Garber wrote:
Quoted text · 18 lines
Hello internals,
__get() and __set() are great, but 90% of the time, I find myself using them to create public readonly properties.
The only problem with this is it is horridly inefficient, consuming at least 1 function call and one switch statement (or equiv) per property read.
Would it be possible to create a new object property attribute: readonly
class xx
{
readonly $bar;
}$o = new xx();
$o->bar = 10;
FATAL ERROR
This way, PHP would allow reading (as if it were public), but only allow writing from within the class.
I think it could really boost performance of complicated application logic that wishes to enforce good visibility.
Comments?
PS. What brought this up was some serious performance issues in a piece of code that I am working with - most of which can be tied back to __get() performance.
--
Best regards,
Jason Garber mailto:jason@ionzoft.com IonZoft, Inc.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: RIPEMD160
Zeev Suraski wrote:
I have to say that in second thought (after realizing what you really meant) it sounds like a very useful feature. [snip]
In order to push complexity down I'd avoid making this yet another modifier, and in fact make this an access level, on par with private/protected/public, that would behave like public, except for when outside the object scope (i.e., have it between protected and public). One of the trickey things would be finding an acceptable name, since 'readonly' implies something which this variable isn't (it's very much writable, from the right places). Maybe something like 'visible' (of course preferably we need to find something that begins with 'p'...)
How about... 'package' ;)
Jasper
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQFEaPIZFfAeHhDzT4gRA6MwAJ95DrFGcKmn724VR2y8kaKrpNychwCgm4Ep U84/fYGcy2JjbwlIBBlBBqI=
=G9a4
-----END PGP SIGNATURE
Hello Zeev,
In the same way that public readonly properties would be useful from the global scope, protected readonly properties would be just as useful to those of us who spend their php-lives writing base classes (like me) for others to extend and use.
I would imagine that the Zend Framework will encounter the (performance based) need for this eventually - I already have.
That being said, an access level that means "public readonly" would be very good - but taking it the whole way would be a lot better.
Considering that it is an optional keyword, and will only be used where __get(), __set() used to be used - it won't confuse the end users who do not care about it. (get/set is a lot more complex).
Thanks!
--
Best regards,
Jason mailto:jason@ionzoft.com
Monday, May 15, 2006, 2:15:50 PM, you wrote:
ZS> I have to say that in second thought (after realizing what you really ZS> meant) it sounds like a very useful feature.
ZS> The main thing that worries me is the complexity to the end user, ZS> which is already in a pretty bad shape as it is today, and many ZS> people here care very little about it, because they can't relate to ZS> beginners and average developers. Private/protected/public is a ZS> challenge to many of them (not the theory, real world situations), ZS> doubling complexity with a modifier is not a good idea.
ZS> In order to push complexity down I'd avoid making this yet another ZS> modifier, and in fact make this an access level, on par with ZS> private/protected/public, that would behave like public, except for ZS> when outside the object scope (i.e., have it between protected and ZS> public). One of the trickey things would be finding an acceptable ZS> name, since 'readonly' implies something which this variable isn't ZS> (it's very much writable, from the right places). Maybe something ZS> like 'visible' (of course preferably we need to find something that ZS> begins with 'p'...)
ZS> Zeev
ZS> At 02:35 12/05/2006, Jason Garber wrote:
Quoted text · 18 lines
Hello internals,
__get() and __set() are great, but 90% of the time, I find myself using them to create public readonly properties.
The only problem with this is it is horridly inefficient, consuming at least 1 function call and one switch statement (or equiv) per property read.
Would it be possible to create a new object property attribute: readonly
class xx
{
readonly $bar;
}$o = new xx();
$o->bar = 10;
FATAL ERROR
This way, PHP would allow reading (as if it were public), but only allow writing from within the class.
I think it could really boost performance of complicated application logic that wishes to enforce good visibility.
Comments?
PS. What brought this up was some serious performance issues in a piece of code that I am working with - most of which can be tied back to __get() performance.
--
Best regards,
Jason Garber mailto:jason@ionzoft.com IonZoft, Inc.
Would a method be able to return a writable reference of this readonly property? If not it'd actually be PITA to prevent this. This is one of those questions which can turn a two second patch by Marcus, to an ongoing patch where we continue to fix various places and continue to bloat the language and make it much harder to maintain (& possibly create new inconsistencies). So I suggest that before we run forward it has to be clear exactly what the semantics would be and in what cases it might fail. Then we should consider whether we adopt or reject the idea. Not saying it's a bad idea but as I've said many times in the past, running ahead with an idea like this, without thinking through the implications and edge case semantics is a problem...
At 02:43 PM 5/15/2006, Jason Garber wrote:
Quoted text · 33 lines
Hello Zeev,
In the same way that public readonly properties would be useful from the global scope, protected readonly properties would be just as useful to those of us who spend their php-lives writing base classes (like me) for others to extend and use.
I would imagine that the Zend Framework will encounter the (performance based) need for this eventually - I already have.
That being said, an access level that means "public readonly" would be very good - but taking it the whole way would be a lot better.
Considering that it is an optional keyword, and will only be used where __get(), __set() used to be used - it won't confuse the end users who do not care about it. (get/set is a lot more complex).
Thanks!
--
Best regards,
Jason mailto:jason@ionzoft.comMonday, May 15, 2006, 2:15:50 PM, you wrote:
ZS> I have to say that in second thought (after realizing what you really ZS> meant) it sounds like a very useful feature.
ZS> The main thing that worries me is the complexity to the end user, ZS> which is already in a pretty bad shape as it is today, and many ZS> people here care very little about it, because they can't relate to ZS> beginners and average developers. Private/protected/public is a ZS> challenge to many of them (not the theory, real world situations), ZS> doubling complexity with a modifier is not a good idea.
ZS> In order to push complexity down I'd avoid making this yet another ZS> modifier, and in fact make this an access level, on par with ZS> private/protected/public, that would behave like public, except for ZS> when outside the object scope (i.e., have it between protected and ZS> public). One of the trickey things would be finding an acceptable ZS> name, since 'readonly' implies something which this variable isn't ZS> (it's very much writable, from the right places). Maybe something ZS> like 'visible' (of course preferably we need to find something that ZS> begins with 'p'...)
ZS> Zeev
ZS> At 02:35 12/05/2006, Jason Garber wrote:
Hello internals,
__get() and __set() are great, but 90% of the time, I find myself using them to create public readonly properties.
The only problem with this is it is horridly inefficient, consuming at least 1 function call and one switch statement (or equiv) per property read.
Would it be possible to create a new object property attribute: readonly
class xx
{
readonly $bar;
}$o = new xx();
$o->bar = 10;
FATAL ERROR
This way, PHP would allow reading (as if it were public), but only allow writing from within the class.
I think it could really boost performance of complicated application logic that wishes to enforce good visibility.
Comments?
PS. What brought this up was some serious performance issues in a piece of code that I am working with - most of which can be tied back to __get() performance.
--
Best regards,
Jason Garber mailto:jason@ionzoft.com IonZoft, Inc.
I suggest to wait for a response about the edge cases question I sent out (i.e. returning a writable reference of this readonly property). I for one haven't quite understood what the semantics will be in all the edge case situations. Although I think the concept is interesting (and somewhat beneficial), I don't want to regret having done it 6 months down the line when people complain it's broken because in edge cases it doesn't work. We need to be sure that the semantics are 100% ok in all cases. Writing a patch is the easy part. The fact that patches can be written doesn't mean we shouldn't be careful about making these changes. We have made too many mistakes in the past which were hard to undo.
Andi
At 07:12 PM 5/15/2006, Andi Gutmans wrote:
Quoted text · 35 lines
Would a method be able to return a writable reference of this readonly property? If not it'd actually be PITA to prevent this. This is one of those questions which can turn a two second patch by Marcus, to an ongoing patch where we continue to fix various places and continue to bloat the language and make it much harder to maintain (& possibly create new inconsistencies). So I suggest that before we run forward it has to be clear exactly what the semantics would be and in what cases it might fail. Then we should consider whether we adopt or reject the idea. Not saying it's a bad idea but as I've said many times in the past, running ahead with an idea like this, without thinking through the implications and edge case semantics is a problem...
At 02:43 PM 5/15/2006, Jason Garber wrote:
Hello Zeev,
In the same way that public readonly properties would be useful from the global scope, protected readonly properties would be just as useful to those of us who spend their php-lives writing base classes (like me) for others to extend and use.
I would imagine that the Zend Framework will encounter the (performance based) need for this eventually - I already have.
That being said, an access level that means "public readonly" would be very good - but taking it the whole way would be a lot better.
Considering that it is an optional keyword, and will only be used where __get(), __set() used to be used - it won't confuse the end users who do not care about it. (get/set is a lot more complex).
Thanks!
--
Best regards,
Jason mailto:jason@ionzoft.comMonday, May 15, 2006, 2:15:50 PM, you wrote:
ZS> I have to say that in second thought (after realizing what you really ZS> meant) it sounds like a very useful feature.
ZS> The main thing that worries me is the complexity to the end user, ZS> which is already in a pretty bad shape as it is today, and many ZS> people here care very little about it, because they can't relate to ZS> beginners and average developers. Private/protected/public is a ZS> challenge to many of them (not the theory, real world situations), ZS> doubling complexity with a modifier is not a good idea.
ZS> In order to push complexity down I'd avoid making this yet another ZS> modifier, and in fact make this an access level, on par with ZS> private/protected/public, that would behave like public, except for ZS> when outside the object scope (i.e., have it between protected and ZS> public). One of the trickey things would be finding an acceptable ZS> name, since 'readonly' implies something which this variable isn't ZS> (it's very much writable, from the right places). Maybe something ZS> like 'visible' (of course preferably we need to find something that ZS> begins with 'p'...)
ZS> Zeev
ZS> At 02:35 12/05/2006, Jason Garber wrote:
Hello internals,
__get() and __set() are great, but 90% of the time, I find myself using them to create public readonly properties.
The only problem with this is it is horridly inefficient, consuming at least 1 function call and one switch statement (or equiv) per property read.
Would it be possible to create a new object property attribute: readonly
class xx
{
readonly $bar;
}$o = new xx();
$o->bar = 10;
FATAL ERROR
This way, PHP would allow reading (as if it were public), but only allow writing from within the class.
I think it could really boost performance of complicated application logic that wishes to enforce good visibility.
Comments?
PS. What brought this up was some serious performance issues in a piece of code that I am working with - most of which can be tied back to __get() performance.
--
Best regards,
Jason Garber mailto:jason@ionzoft.com IonZoft, Inc.
Hello Andi,
however the past has tought of something. Nobody will test a patch. That said there won't be any feedback until it gets into at least head and people feel confident they should experiment with it. As it stands now you could just take it as a decision against it and be done.
best regards
marcus
Tuesday, May 16, 2006, 10:39:53 PM, you wrote:
I suggest to wait for a response about the edge cases question I sent out (i.e. returning a writable reference of this readonly property).
How is a writeable reference a public read variable in php? In php we don't have concept like c++ offeres and even there normally const methods return const references aka read only references.
I for one haven't quite understood what the semantics will be in all the edge case situations.
The semantics for the majority seems to be public read. E.g. the language offers a way to circumvent or redefine visibility when reading.
Although I think the concept is interesting (and somewhat beneficial), I don't want to regret having done it 6 months down the line when people complain it's broken because in edge cases it doesn't work. We need to be sure that the semantics are 100% ok in all cases. Writing a patch is the easy part.
If that was the case, then why was i the only one to provide a patch like in so many times before. I mean yes it is easy for me, and definitively also for dmitry and sara as well as most likely for you too. But how mayn other people can provide such a patch?
The fact that
patches can be written doesn't mean we shouldn't be careful about making these changes. We have made too many mistakes in the past which were hard to undo.
Yes we have only i don't know what we can really do about. I mean other then discussiong it, see that it is doable and getting a feeling for it? We can apply it or forget it.
best regards
marcus
p.s.: I still have the patches for public/protected/private and those for final and tons of others related to our object model. Want me to use them to drop any feature? :-))
Hi Marcus,
I don't see why everything is always personal for you. As I mentioned numerous times in the past, having an in-depth discussion about features, both their implementation, affect on language semantics, and relevance to PHP is very beneficial for all. All I meant was that the implementation is one step (and a very necessary step) but that having a patch doesn't always mean it should be applied right away. I just want to make sure we all think it through and discuss the different cases. You seem to be very hesitant around any discussion of such patches which I don't quite understand. It's not geared towards you but the right thing for a mature project like PHP to do.
So it boils down again to the same very relevant question I asked initially. Where do you see the edge cases? Can I return a readonly property by reference, can I assign it by reference? What kind of internal functions/classes need fixing? I think thinking these things through (and I believe you can find the answers for these questions) is the process we should really have in PHP. Such major patches should also discuss the dependencies. What we tend to do is to first commit, and then fix later over a period of a year sometimes even discovering down the road that we have conceptual issues.
So I'm really not asking for too much. Just asking to also work on the second part which is defining better the reach of this patch. I think this part needs at least as much effort as the patch itself.
Andi
At 01:55 PM 5/16/2006, Marcus Boerger wrote:
Quoted text · 17 lines
Hello Andi,
however the past has tought of something. Nobody will test a patch. That said there won't be any feedback until it gets into at least head and people feel confident they should experiment with it. As it stands now you could just take it as a decision against it and be done.
best regards
marcusTuesday, May 16, 2006, 10:39:53 PM, you wrote:
I suggest to wait for a response about the edge cases question I sent out (i.e. returning a writable reference of this readonly property).
How is a writeable reference a public read variable in php? In php we don't have concept like c++ offeres and even there normally const methods return const references aka read only references.
I for one haven't quite understood what the semantics will be in all the edge case situations.
The semantics for the majority seems to be public read. E.g. the language offers a way to circumvent or redefine visibility when reading.
Although I think the concept is interesting (and somewhat beneficial), I don't want to regret having done it 6 months down the line when people complain it's broken because in edge cases it doesn't work. We need to be sure that the semantics are 100% ok in all cases. Writing a patch is the easy part.
If that was the case, then why was i the only one to provide a patch like in so many times before. I mean yes it is easy for me, and definitively also for dmitry and sara as well as most likely for you too. But how mayn other people can provide such a patch?
The fact that
patches can be written doesn't mean we shouldn't be careful about making these changes. We have made too many mistakes in the past which were hard to undo.Yes we have only i don't know what we can really do about. I mean other then discussiong it, see that it is doable and getting a feeling for it? We can apply it or forget it.
best regards
marcusp.s.: I still have the patches for public/protected/private and those for final and tons of others related to our object model. Want me to use them to drop any feature? :-))
Hello Andi,
Tuesday, May 16, 2006, 11:09:51 PM, you wrote:
Hi Marcus,
I don't see why everything is always personal for you.
Na. Don't get me wrong here. I am just trying to express that what we did in the past didn't really work out. The conservative behavior of adding new features late if at all resulted in a few problems we might have been able to avoid had we added the stuff earlier. Howver adding everything early is as you correctly said unmaintainable. And i further more have no alternative.
As I mentioned
numerous times in the past, having an in-depth discussion about features, both their implementation, affect on language semantics, and relevance to PHP is very beneficial for all. All I meant was that the implementation is one step (and a very necessary step) but that having a patch doesn't always mean it should be applied right away. I just want to make sure we all think it through and discuss the different cases.
Discussing it is good as long as we actually agree that it is worth discussing :-)
So it boils down again to the same very relevant question I asked initially. Where do you see the edge cases? Can I return a readonly property by reference, can I assign it by reference?
Nope, no references. References would allow writing. Because PHP doesn't offer readonly variable at all. Which is another reason i went the 'readable' road.
So I'm really not asking for too much. Just asking to also work on the second part which is defining better the reach of this patch. I think this part needs at least as much effort as the patch itself.
I do all i can. I try to get it straight, find a nice semantics buld up my own opinion and the try to convince people of it. Besides that as always i try to correct any technical or theoretical issue i can stand helping more people to understand the matters :-) It's just little but all i can do, sorry :-)
Best regards,
Marcus
Andi Gutmans wrote:
All I meant was that the implementation is one step (and a very necessary step) but that having a patch doesn't always mean it should be applied right away. I just want to make sure we all think it through and discuss the different cases.
IMHO, it is very cumbersome to follow the discussion of both syntax and semantics implications of such a change to the language on a mailinglist alone.
I think a text document maintained in CVS is much better suited for this as it gives you one place to look at for the current state of the discussion (and you can look at the CVS history to see how it evolved).
--
Sebastian Bergmann http://www.sebastian-bergmann.de/ GnuPG Key: 0xB85B5D69 / 27A7 2B14 09E4 98CD 6277 0E5B 6867 C514 B85B 5D69
This is absolutely true. Besides, adding a new access level automatically implies that you'll probably never be able to extend it to protected as well, because you'd need yet another access level and I really doubt people are going to want to do that. But at that point, it would be too late to revert back to a readonly keyword, since the whole world is using the new publicly readable access level already. Choosing to go for a new access level can turn out to be a really big mistake in the long run.
Like you, I don't see why a 'readable' keyword should make things any more complicated for beginners, because indeed, it is optional and beginners will simply not use it. To me, this only shows the strength of PHP: suitable for beginners and suitable for the enterprise.
Ron
"Jason Garber" jason@ionzoft.com schreef in bericht news:1217741491.20060515174322@ionzoft.com...
Quoted text · 33 lines
Hello Zeev,
In the same way that public readonly properties would be useful from the global scope, protected readonly properties would be just as useful to those of us who spend their php-lives writing base classes (like me) for others to extend and use.
I would imagine that the Zend Framework will encounter the (performance based) need for this eventually - I already have.
That being said, an access level that means "public readonly" would be very good - but taking it the whole way would be a lot better.
Considering that it is an optional keyword, and will only be used where __get(), __set() used to be used - it won't confuse the end users who do not care about it. (get/set is a lot more complex).
Thanks!
--
Best regards,
Jason mailto:jason@ionzoft.comMonday, May 15, 2006, 2:15:50 PM, you wrote:
ZS> I have to say that in second thought (after realizing what you really ZS> meant) it sounds like a very useful feature.
ZS> The main thing that worries me is the complexity to the end user, ZS> which is already in a pretty bad shape as it is today, and many ZS> people here care very little about it, because they can't relate to ZS> beginners and average developers. Private/protected/public is a ZS> challenge to many of them (not the theory, real world situations), ZS> doubling complexity with a modifier is not a good idea.
ZS> In order to push complexity down I'd avoid making this yet another ZS> modifier, and in fact make this an access level, on par with ZS> private/protected/public, that would behave like public, except for ZS> when outside the object scope (i.e., have it between protected and ZS> public). One of the trickey things would be finding an acceptable ZS> name, since 'readonly' implies something which this variable isn't ZS> (it's very much writable, from the right places). Maybe something ZS> like 'visible' (of course preferably we need to find something that ZS> begins with 'p'...)
ZS> Zeev
ZS> At 02:35 12/05/2006, Jason Garber wrote:
Hello internals,
__get() and __set() are great, but 90% of the time, I find myself using them to create public readonly properties.
The only problem with this is it is horridly inefficient, consuming at least 1 function call and one switch statement (or equiv) per property read.
Would it be possible to create a new object property attribute: readonly
class xx
{
readonly $bar;
}$o = new xx();
$o->bar = 10;
FATAL ERROR
This way, PHP would allow reading (as if it were public), but only allow writing from within the class.
I think it could really boost performance of complicated application logic that wishes to enforce good visibility.
Comments?
PS. What brought this up was some serious performance issues in a piece of code that I am working with - most of which can be tied back to __get() performance.
--
Best regards,
Jason Garber mailto:jason@ionzoft.com IonZoft, Inc.
Ron Korving wrote:
Like you, I don't see why a 'readable' keyword should make things any more complicated for beginners, because indeed, it is optional and beginners will simply not use it. To me, this only shows the strength of PHP: suitable for beginners and suitable for the enterprise.
Learning the language is getting more and more difficult. Defining a variable used to be "var" (or no declaration at all). Then visibility was added (and choosing the right visibility is trickier not only for beginners than you might think), as was "static" and "final". Your argument that all the new constructs can be ignored if not needed has two flaws:
a) The language reference gets bloated
b) Learning by example (one of the selling points of PHP originally IMHO) is made harder than necessary
"Suitable for the enterprise" to me is a bit like "Linux, ready for the desktop": It's missing the point by trying to fit everybody's needs. What happened to the KISS principle and Unix philosophy of doing one thing and doing it right?
Just my two cents,
- Chris
Ron,
As I've said numerous times in the past, it's not about what beginners use or not, it's about whether it's in the language or not.
Additional keywords and structures make the language more complicated. It's an axiom. Look at PHP 5 books vs. PHP 4 books, including books for beginners. All of them cover everything, and for a good reason - in the PHP world, you need to be able to understand other people's code at least as much as you have to be able to create new code yourself. Every new construct, every new access level or modifier - become a standard part of the basic-elements that PHP developers have to understand and be aware of. So the 'beginners won't use it' argument simply doesn't hold water, as it will confuse them irregardless. I'm seeing that in practice, in places far, far away from internals@, and there are definitely doubts and worries amongst PHP developers regarding the complexity&purity directions that PHP appears to be taking. Most of the people I talk with are mostly happy with PHP 5 (although there are those who think that PHP 5 already went too far), but a lot of them say that we should not go any further in order to preserve the simplicity.
Now, all that said, I'm not certain what's the best option here. In terms of complexity, clearly not adding this modifier is the best approach. There's also the issue Andi mentioned, of the feasibility of creating this patch and maintaining it in an acceptable manner. Assuming we all agree to live forever with the semantics that can be implemented, I for one think it's actually a fairly useful feature, including very much for beginners.
Regarding whether it's an access level or a modifier, I think that an access level is better since you don't have to explain a matrix of 6 different behaviors (including one that doesn't make sense, but still requires an explanation), only one more access level. I think we can definitely give up the equivalent of 'readonly protected', and tell people to use protected and restrain themselves in those rare cases where it's needed and use regular protected or __set(). When we make this decision, though, it means that we won't be adding the equivalent of 'readonly protected' in the future either, including when people come along and present a use case where it'd be great to use it.
Zeev
At 11:44 16/05/2006, Ron Korving wrote:
Quoted text · 37 lines
This is absolutely true. Besides, adding a new access level automatically implies that you'll probably never be able to extend it to protected as well, because you'd need yet another access level and I really doubt people are going to want to do that. But at that point, it would be too late to revert back to a readonly keyword, since the whole world is using the new publicly readable access level already. Choosing to go for a new access level can turn out to be a really big mistake in the long run.
Like you, I don't see why a 'readable' keyword should make things any more complicated for beginners, because indeed, it is optional and beginners will simply not use it. To me, this only shows the strength of PHP: suitable for beginners and suitable for the enterprise.
Ron
"Jason Garber" jason@ionzoft.com schreef in bericht news:1217741491.20060515174322@ionzoft.com...
Hello Zeev,
In the same way that public readonly properties would be useful from the global scope, protected readonly properties would be just as useful to those of us who spend their php-lives writing base classes (like me) for others to extend and use.
I would imagine that the Zend Framework will encounter the (performance based) need for this eventually - I already have.
That being said, an access level that means "public readonly" would be very good - but taking it the whole way would be a lot better.
Considering that it is an optional keyword, and will only be used where __get(), __set() used to be used - it won't confuse the end users who do not care about it. (get/set is a lot more complex).
Thanks!
--
Best regards,
Jason mailto:jason@ionzoft.comMonday, May 15, 2006, 2:15:50 PM, you wrote:
ZS> I have to say that in second thought (after realizing what you really ZS> meant) it sounds like a very useful feature.
ZS> The main thing that worries me is the complexity to the end user, ZS> which is already in a pretty bad shape as it is today, and many ZS> people here care very little about it, because they can't relate to ZS> beginners and average developers. Private/protected/public is a ZS> challenge to many of them (not the theory, real world situations), ZS> doubling complexity with a modifier is not a good idea.
ZS> In order to push complexity down I'd avoid making this yet another ZS> modifier, and in fact make this an access level, on par with ZS> private/protected/public, that would behave like public, except for ZS> when outside the object scope (i.e., have it between protected and ZS> public). One of the trickey things would be finding an acceptable ZS> name, since 'readonly' implies something which this variable isn't ZS> (it's very much writable, from the right places). Maybe something ZS> like 'visible' (of course preferably we need to find something that ZS> begins with 'p'...)
ZS> Zeev
ZS> At 02:35 12/05/2006, Jason Garber wrote:
Hello internals,
__get() and __set() are great, but 90% of the time, I find myself using them to create public readonly properties.
The only problem with this is it is horridly inefficient, consuming at least 1 function call and one switch statement (or equiv) per property read.
Would it be possible to create a new object property attribute: readonly
class xx
{
readonly $bar;
}$o = new xx();
$o->bar = 10;
FATAL ERROR
This way, PHP would allow reading (as if it were public), but only allow writing from within the class.
I think it could really boost performance of complicated application logic that wishes to enforce good visibility.
Comments?
PS. What brought this up was some serious performance issues in a piece of code that I am working with - most of which can be tied back to __get() performance.
--
Best regards,
Jason Garber mailto:jason@ionzoft.com IonZoft, Inc.
"Zeev Suraski" zeev@zend.com schreef in bericht news:7.0.1.0.2.20060516133953.09045a30@zend.com...
...
Regarding whether it's an access level or a modifier, I think that an access level is better since you don't have to explain a matrix of 6 different behaviors (including one that doesn't make sense, but still requires an explanation), only one more access level. I think we can definitely give up the equivalent of 'readonly protected', and tell people to use protected and restrain themselves in those rare cases where it's needed and use regular protected or __set(). When we make this decision, though, it means that we won't be adding the equivalent of 'readonly protected' in the future either, including when people come along and present a use case where it'd be great to use it.Zeev
I don't need this feature, so I'm not suggestion anything just because I'd prefer it that way myself. I'm perfectly happy writing setters and getters. The only reason of my post was that I anticipate disappointment in the long run when, as you confirm yourself, "readonly protected" will become practically impossible (besides which, going beyond ppp just seems a little ugly, but I'm sure that's just a matter of taste).
I don't really see the huge deal in that a beginner has to be able to understand every bit of code. That same problem exists with other languages where beginners will have big problems diving into complex code (very much including syntax related complexity, like with c++ templates, macros, ...).
The only thing I fear is disappointment in the long run really, because an irriversible unlucky decision was made (magic quotes, parameter order of certain functions).
In fact, if you want the easiest solution, I actually think the wrong idea I had about c++ access levels could prove to be useful:
What if access levels only mean anything to writing to data members, while all data members are publicly readable?
<?php
class Foo
{
private $bar = 5;
}
$foo = new Foo();
echo $foo->bar; // prints 5
$foo->bar = 4; // error
?>
I think backwards compatibility would likely be alright, performance will be fine, there will be no extra keyword, no extra access level, and no need for getter functions. Just an idea.
Regards,
Ron
Quoted text · 43 lines
At 11:44 16/05/2006, Ron Korving wrote:
This is absolutely true. Besides, adding a new access level automatically implies that you'll probably never be able to extend it to protected as well, because you'd need yet another access level and I really doubt people
are going to want to do that. But at that point, it would be too late to revert back to a readonly keyword, since the whole world is using the new publicly readable access level already. Choosing to go for a new access level can turn out to be a really big mistake in the long run.Like you, I don't see why a 'readable' keyword should make things any more complicated for beginners, because indeed, it is optional and beginners will
simply not use it. To me, this only shows the strength of PHP: suitable for
beginners and suitable for the enterprise.Ron
"Jason Garber" jason@ionzoft.com schreef in bericht news:1217741491.20060515174322@ionzoft.com...
Hello Zeev,
In the same way that public readonly properties would be useful from the global scope, protected readonly properties would be just as useful to those of us who spend their php-lives writing base classes (like me) for others to extend and use.
I would imagine that the Zend Framework will encounter the (performance based) need for this eventually - I already have.
That being said, an access level that means "public readonly" would be very good - but taking it the whole way would be a lot better.
Considering that it is an optional keyword, and will only be used where __get(), __set() used to be used - it won't confuse the end users who do not care about it. (get/set is a lot more complex).
Thanks!
--
Best regards,
Jason mailto:jason@ionzoft.comMonday, May 15, 2006, 2:15:50 PM, you wrote:
ZS> I have to say that in second thought (after realizing what you really
ZS> meant) it sounds like a very useful feature.ZS> The main thing that worries me is the complexity to the end user, ZS> which is already in a pretty bad shape as it is today, and many ZS> people here care very little about it, because they can't relate to ZS> beginners and average developers. Private/protected/public is a ZS> challenge to many of them (not the theory, real world situations), ZS> doubling complexity with a modifier is not a good idea.
ZS> In order to push complexity down I'd avoid making this yet another ZS> modifier, and in fact make this an access level, on par with ZS> private/protected/public, that would behave like public, except for ZS> when outside the object scope (i.e., have it between protected and ZS> public). One of the trickey things would be finding an acceptable ZS> name, since 'readonly' implies something which this variable isn't ZS> (it's very much writable, from the right places). Maybe something ZS> like 'visible' (of course preferably we need to find something that ZS> begins with 'p'...)
ZS> Zeev
ZS> At 02:35 12/05/2006, Jason Garber wrote:
Hello internals,
__get() and __set() are great, but 90% of the time, I find myself using them to create public readonly properties.
The only problem with this is it is horridly inefficient, consuming at least 1 function call and one switch statement (or equiv) per property read.
Would it be possible to create a new object property attribute: readonly
class xx
{
readonly $bar;
}$o = new xx();
$o->bar = 10;
FATAL ERROR
This way, PHP would allow reading (as if it were public), but only allow writing from within the class.
I think it could really boost performance of complicated application
logic that wishes to enforce good visibility.Comments?
PS. What brought this up was some serious performance issues in a piece of code that I am working with - most of which can be tied back to __get() performance.
--
Best regards,
Jason Garber mailto:jason@ionzoft.com IonZoft, Inc.
Quoted text · 9 lines
<?php
class Foo
{
private $bar = 5;
}$foo = new Foo();
echo $foo->bar; // prints 5
$foo->bar = 4; // error?>
I'm against visible private variables. If they are visible, they are part of the interface of the class, which means changing the implementation means a BC break. If this functionality is going in, please do it only for protected...
Regards,
Stefan
That's a good point. It was just an idea really, hoping to provide an easy way out for the whole discussion ;)
Ron
""Stefan Walk"" stefan.walk@gmail.com schreef in bericht news:4858f9d90605160625u11fcaef8ta07fc199558eca9f@mail.gmail.com...
Quoted text · 9 lines
<?php
class Foo
{
private $bar = 5;
}$foo = new Foo();
echo $foo->bar; // prints 5
$foo->bar = 4; // error?>
I'm against visible private variables. If they are visible, they are part of the interface of the class, which means changing the implementation means a BC break. If this functionality is going in, please do it only for protected...
Regards,
Stefan
Stefan Walk wrote:
I'm against visible private variables. If they are visible, they are part of the interface of the class, which means changing the implementation means a BC break.
This is a valid point against readonly, indeed.
--
Sebastian Bergmann http://www.sebastian-bergmann.de/ GnuPG Key: 0xB85B5D69 / 27A7 2B14 09E4 98CD 6277 0E5B 6867 C514 B85B 5D69
Sebastian Bergmann wrote:
Stefan Walk wrote:
I'm against visible private variables. If they are visible, they are part of the interface of the class, which means changing the implementation means a BC break.
This is a valid point against readonly, indeed.
no, it is a valid point against implicit visibility, not against declaring a property as readonly
--
Hartmut Holzgraefe, Senior Support Engineer . MySQL AB, www.mysql.com
Are you certified? http://www.mysql.com/training/certification
Quoted text · 4 lines
Like you, I don't see why a 'readable' keyword should make things any more
complicated for beginners, because indeed, it is optional and beginners will
simply not use it. To me, this only shows the strength of PHP: suitable for
beginners and suitable for the enterprise.
To be even more expansive:
At whatever level of understanding one has for private/protected/public, I don't think the prefix 'readonly' is going to add any significant complexity to that understanding.
The people who have no clue and don't care about all this OOP stuff can ignore that.
The goofballs who abuse and mis-use private/protected/public because some book told them it was "good" will be equally liberal with their use of "readonly" and only have themselves to blame when they've painted themselves into a corner (again)
The 10% of PHP coders who actually understand p/p/p and even apply it in arguably useful ways will grok readonly just fine.
:-)
Sufficient examples of the utility of 'readonly' with all levels of p/p/p have convinced this village idiot that it should be a modifier to p/p/p and not some new 'p' wedged in.
--
Like Music?
http://l-i-e.com/artists.htm