#49526 [Com]: C# style property get/set syntax
ID: 49526 Comment by: president at basnetworks dot net Reported By: president at basnetworks dot net Status: Open Bug Type:Feature/Change Request PHP Version: 6SVN-2009-09-11 (SVN) New Comment: For reference, the RFC for this feature request now exists at this URL: http://wiki.php.net/rfc/propertygetsetsyntax Previous Comments: [2009-09-13 04:24:08] president at basnetworks dot net Hi Kalle, Thanks for the information. I will follow your instructions and start working on the RFC over the next week(s). Also, thanks for the link about the Developer Summit, but it does not show the outcome of the discussion, am I able to find that anywhere? [2009-09-12 12:22:19] ka...@php.net Hey The best way to request this feature is to write an RFC in our wiki at: http://wiki.php.net/rfc/ Request an account at: http://wiki.php.net/start?do=register And then send an email to the webmaster list (php-webmas...@lists.php.net) requesting write access to the rfc namespace and repost this feature request as an RFC. When you are done, start a new thread on internals (intern...@lists.php.net) explaining your RFC. As for reference, this idea was discussed at the last PDM in May, see: http://wiki.php.net/summits/pdmnotesmay09#php_6 point #16 [2009-09-11 01:02:26] president at basnetworks dot net Description: I would like to request a C# style get/set syntax (called a property in C#) for PHP. Basically, it looks and acts like a class member/variable from outside the class, but it is actually a set of two methods. It can be used to provide only read or write access to a class member, do pre or post processing on a member, or be completely dynamic like a set of class methods. A property contains two methods between braces, named get and set. get must always have a return statement, while set has a magic variable value or $value which is the variable that was passed to the property. Either method can be omitted to make the property read-only or write-only. The same effect can be achieved by creating a GetVar() and SetVar() method to create a sudo-property var, although it is by far much clumsier and less intuitive. I also realize the same effect received outside the class can be achieved using the __get() and __set() methods, but these methods are only really useful in a small instance of situations, like giving access to an internal array as though each index is a property. These magic methods are not at all useful for using on an individual property basis, and it gets worse when inheritance is introduced. The C# syntax is as follows: class TimePeriod { private double seconds; public double Hours { get { return seconds / 3600; } set { seconds = value * 3600; } } } The PHP syntax would be similar to the following: class TimePeriod { private $seconds; public property Hours { get { return $this-seconds / 3600; } set { $this-seconds = $value * 3600; } } } You would use it exactly the same as a public class member: $time = new TimePeriod(); $time-Hours = 24; echo $time-Hours; As opposed to the alternative: $time = new TimePeriod(); $time-SetHours(24); echo $time-GetHours(); Additionally, the get and set methods can have separate visibilities like in the following example where get is public and set is protected: public property Name { get { return $this-name; } protected set { $this-name = $value; } } There is another ticket that is similar but not the same thing here: http://bugs.php.net/bug.php?id=34194 It suggests separate getter/setter methods, which in my opinion are much less intuitive. I believe that following the C# format would help to keep a standard format, and would be the least confusing. The poster of that bug also fails to realize that separate visibility levels can be achieved for properties using the C# syntax, as shown above. The C# documentation on properties is available here: http://msdn.microsoft.com/en-us/library/x9fsa0sw%28VS.80%29.aspx The C# documentation on Asymmetric Accessor Accessibility for properties is available here: http://msdn.microsoft.com/en-us/library/75e8y5dd%28VS.80%29.aspx -- Edit this bug report at http://bugs.php.net/?id=49526edit=1
#49526 [Com]: C# style property get/set syntax
ID: 49526 Comment by: president at basnetworks dot net Reported By: president at basnetworks dot net Status: Open Bug Type:Feature/Change Request PHP Version: 6SVN-2009-09-11 (SVN) New Comment: Hi Kalle, Thanks for the information. I will follow your instructions and start working on the RFC over the next week(s). Also, thanks for the link about the Developer Summit, but it does not show the outcome of the discussion, am I able to find that anywhere? Previous Comments: [2009-09-12 12:22:19] ka...@php.net Hey The best way to request this feature is to write an RFC in our wiki at: http://wiki.php.net/rfc/ Request an account at: http://wiki.php.net/start?do=register And then send an email to the webmaster list (php-webmas...@lists.php.net) requesting write access to the rfc namespace and repost this feature request as an RFC. When you are done, start a new thread on internals (intern...@lists.php.net) explaining your RFC. As for reference, this idea was discussed at the last PDM in May, see: http://wiki.php.net/summits/pdmnotesmay09#php_6 point #16 [2009-09-11 01:02:26] president at basnetworks dot net Description: I would like to request a C# style get/set syntax (called a property in C#) for PHP. Basically, it looks and acts like a class member/variable from outside the class, but it is actually a set of two methods. It can be used to provide only read or write access to a class member, do pre or post processing on a member, or be completely dynamic like a set of class methods. A property contains two methods between braces, named get and set. get must always have a return statement, while set has a magic variable value or $value which is the variable that was passed to the property. Either method can be omitted to make the property read-only or write-only. The same effect can be achieved by creating a GetVar() and SetVar() method to create a sudo-property var, although it is by far much clumsier and less intuitive. I also realize the same effect received outside the class can be achieved using the __get() and __set() methods, but these methods are only really useful in a small instance of situations, like giving access to an internal array as though each index is a property. These magic methods are not at all useful for using on an individual property basis, and it gets worse when inheritance is introduced. The C# syntax is as follows: class TimePeriod { private double seconds; public double Hours { get { return seconds / 3600; } set { seconds = value * 3600; } } } The PHP syntax would be similar to the following: class TimePeriod { private $seconds; public property Hours { get { return $this-seconds / 3600; } set { $this-seconds = $value * 3600; } } } You would use it exactly the same as a public class member: $time = new TimePeriod(); $time-Hours = 24; echo $time-Hours; As opposed to the alternative: $time = new TimePeriod(); $time-SetHours(24); echo $time-GetHours(); Additionally, the get and set methods can have separate visibilities like in the following example where get is public and set is protected: public property Name { get { return $this-name; } protected set { $this-name = $value; } } There is another ticket that is similar but not the same thing here: http://bugs.php.net/bug.php?id=34194 It suggests separate getter/setter methods, which in my opinion are much less intuitive. I believe that following the C# format would help to keep a standard format, and would be the least confusing. The poster of that bug also fails to realize that separate visibility levels can be achieved for properties using the C# syntax, as shown above. The C# documentation on properties is available here: http://msdn.microsoft.com/en-us/library/x9fsa0sw%28VS.80%29.aspx The C# documentation on Asymmetric Accessor Accessibility for properties is available here: http://msdn.microsoft.com/en-us/library/75e8y5dd%28VS.80%29.aspx -- Edit this bug report at http://bugs.php.net/?id=49526edit=1
#49526 [NEW]: C# style property get/set syntax
From: president at basnetworks dot net Operating system: PHP version: 6SVN-2009-09-11 (SVN) PHP Bug Type: Feature/Change Request Bug description: C# style property get/set syntax Description: I would like to request a C# style get/set syntax (called a property in C#) for PHP. Basically, it looks and acts like a class member/variable from outside the class, but it is actually a set of two methods. It can be used to provide only read or write access to a class member, do pre or post processing on a member, or be completely dynamic like a set of class methods. A property contains two methods between braces, named get and set. get must always have a return statement, while set has a magic variable value or $value which is the variable that was passed to the property. Either method can be omitted to make the property read-only or write-only. The same effect can be achieved by creating a GetVar() and SetVar() method to create a sudo-property var, although it is by far much clumsier and less intuitive. I also realize the same effect received outside the class can be achieved using the __get() and __set() methods, but these methods are only really useful in a small instance of situations, like giving access to an internal array as though each index is a property. These magic methods are not at all useful for using on an individual property basis, and it gets worse when inheritance is introduced. The C# syntax is as follows: class TimePeriod { private double seconds; public double Hours { get { return seconds / 3600; } set { seconds = value * 3600; } } } The PHP syntax would be similar to the following: class TimePeriod { private $seconds; public property Hours { get { return $this-seconds / 3600; } set { $this-seconds = $value * 3600; } } } You would use it exactly the same as a public class member: $time = new TimePeriod(); $time-Hours = 24; echo $time-Hours; As opposed to the alternative: $time = new TimePeriod(); $time-SetHours(24); echo $time-GetHours(); Additionally, the get and set methods can have separate visibilities like in the following example where get is public and set is protected: public property Name { get { return $this-name; } protected set { $this-name = $value; } } There is another ticket that is similar but not the same thing here: http://bugs.php.net/bug.php?id=34194 It suggests separate getter/setter methods, which in my opinion are much less intuitive. I believe that following the C# format would help to keep a standard format, and would be the least confusing. The poster of that bug also fails to realize that separate visibility levels can be achieved for properties using the C# syntax, as shown above. The C# documentation on properties is available here: http://msdn.microsoft.com/en-us/library/x9fsa0sw%28VS.80%29.aspx The C# documentation on Asymmetric Accessor Accessibility for properties is available here: http://msdn.microsoft.com/en-us/library/75e8y5dd%28VS.80%29.aspx -- Edit bug report at http://bugs.php.net/?id=49526edit=1 -- Try a snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=49526r=trysnapshot52 Try a snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=49526r=trysnapshot53 Try a snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=49526r=trysnapshot60 Fixed in SVN: http://bugs.php.net/fix.php?id=49526r=fixed Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=49526r=needdocs Fixed in release: http://bugs.php.net/fix.php?id=49526r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=49526r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=49526r=needscript Try newer version: http://bugs.php.net/fix.php?id=49526r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=49526r=support Expected behavior: http://bugs.php.net/fix.php?id=49526r=notwrong Not enough info: http://bugs.php.net/fix.php?id=49526r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=49526r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=49526r=globals PHP 4 support discontinued: http://bugs.php.net/fix.php?id=49526r=php4 Daylight Savings:http://bugs.php.net/fix.php?id=49526r=dst IIS Stability: http://bugs.php.net/fix.php?id=49526r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=49526r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=49526r=float No Zend Extensions: http://bugs.php.net/fix.php?id=49526r=nozend MySQL Configuration Error: http://bugs.php.net/fix.php?id=49526r=mysqlcfg
#49269 [Opn]: Ternary operator fails on Iterator object when used inside foreach declaration
ID: 49269 User updated by: president at basnetworks dot net -Summary: isset() fails on Iterator object when used inside foreach declaration Reported By: president at basnetworks dot net Status: Open Bug Type: Class/Object related Operating System: All PHP Version: 5.3.0 New Comment: sjoerd-php at linuxonly dot nl, Your code sample is much clearer, and seems to narrow it down to the ternary operator mis-behaving. Thanks for the added clarification, I will update the report. You should also vote that you reproduced the bug above. Previous Comments: [2009-08-16 10:57:44] sjoerd-php at linuxonly dot nl Note that the Iterator in my previous comment sucks and should not be used. [2009-08-16 10:53:07] sjoerd-php at linuxonly dot nl Thank you for your bug report. I could reproduce the behavior and made a code sample to reproduce it: ?php class TestObject implements Iterator { private $first = true; function valid() { if ($this-first) { $this-first = false; return true; } return false; } function current() { } function next() { } function key() { } function rewind() { } } $array_object = new TestObject(); // Without ternary operator, the foreach is entered foreach ($array_object as $item) { echo This works.\n; } // With ternary operator, the foreach is not entered foreach ((true ? $array_object : $array_object) as $item) { die(Good. Expected behavior.\n); } die(Bad. Foreach was skipped.\n); ? [2009-08-16 10:52:20] sjoerd-php at linuxonly dot nl Thank you for your bug report. I could reproduce the behavior and made a code sample to reproduce it: ?php class TestObject implements Iterator { private $first = true; function valid() { if ($this-first) { $this-first = false; return true; } return false; } function current() { } function next() { } function key() { } function rewind() { } } $array_object = new TestObject(); // Without ternary operator, the foreach is entered foreach ($array_object as $item) { echo This works.\n; } // With ternary operator, the foreach is not entered foreach ((true ? $array_object : $array_object) as $item) { die(Good. Expected behavior.\n); } die(Bad. Foreach was skipped.\n); ? [2009-08-16 01:36:43] president at basnetworks dot net After further testing, I have found this bug is stranger than it seems: foreach ((isset($array_object) ? $array_object : array('1', '2', '3')) as $item) { echo $item; } Should either print 'abc' or '123' no matter if isset() is successful or fails. It prints neither. Now I am wondering if it is not isset(), but the ternary operator that is failing. [2009-08-16 01:28:52] president at basnetworks dot net I addition to the reproduce code, the following may help to understand the bug: foreach ($array_object as $item) { echo $item; } Will successfully print abc, while: foreach ((isset($array_object) ? $array_object : array()) as $item) { echo $item; } will not print anything, indicating that isset() is returning false. I hope that helps. The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at http://bugs.php.net/49269 -- Edit this bug report at http://bugs.php.net/?id=49269edit=1
#49269 [NEW]: isset() fails on Iterator object when used inside foreach declaration
From: president at basnetworks dot net Operating system: All PHP version: 5.3.0 PHP Bug Type: Class/Object related Bug description: isset() fails on Iterator object when used inside foreach declaration Description: The function isset() produces an incorrect result when used on an object that implements the Iterator interface, within a foreach declaration. As illustrated below, when used outside of the foreach() declaration, isset() works as expected in the object that implements Iterator interface, returning true, causing the output 'true'. When isset() is used within the foreach() declaration on the same object, it instead returns false, causing an empty array to be used for the foreach() loop. There is no reason the isset() function should return anything different when used within the foreach() declaration block. Reproduce code: --- class NormalClass { public $a, $b, $c; public function __construct() { $this-a = 'a'; $this-b = 'b'; $this-c = 'c'; } } class ArrayClass implements Iterator { private $internal_array; public function __construct() { $this-internal_array = array('a', 'b', 'c'); } public function key() { return key($this-nodes); } public function current() { return current($this-nodes); } public function next() { next($this-nodes); } public function valid() { return (current($this-nodes) !== false) ? true : false; } public function rewind() { reset($this-nodes); } } $array = array('a', 'b', 'c'); $normal_object = new NormalClass(); $array_object = new ArrayClass(); echo Array: . (isset($array) ? 'true' : 'false'); echo \nNormal Object: . (isset($normal_object) ? 'true' : 'false'); echo \nArray Object: . (isset($array_object) ? 'true' : 'false'); echo \nArray: ; foreach ((isset($array) ? $array : array()) as $item) { echo $item; } echo \nNormal Object: ; foreach ((isset($normal_object) ? $normal_object : array()) as $item) { echo $item; } echo \nArray Object: ; foreach ((isset($array_object) ? $array_object : array()) as $item) { echo $item; } Expected result: Array: true Normal Object: true Array Object: true Array: abc Normal Object: abc Array Object: abc Actual result: -- Array: true Normal Object: true Array Object: true Array: abc Normal Object: abc Array Object: -- Edit bug report at http://bugs.php.net/?id=49269edit=1 -- Try a snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=49269r=trysnapshot52 Try a snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=49269r=trysnapshot53 Try a snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=49269r=trysnapshot60 Fixed in SVN: http://bugs.php.net/fix.php?id=49269r=fixed Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=49269r=needdocs Fixed in release: http://bugs.php.net/fix.php?id=49269r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=49269r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=49269r=needscript Try newer version: http://bugs.php.net/fix.php?id=49269r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=49269r=support Expected behavior: http://bugs.php.net/fix.php?id=49269r=notwrong Not enough info: http://bugs.php.net/fix.php?id=49269r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=49269r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=49269r=globals PHP 4 support discontinued: http://bugs.php.net/fix.php?id=49269r=php4 Daylight Savings:http://bugs.php.net/fix.php?id=49269r=dst IIS Stability: http://bugs.php.net/fix.php?id=49269r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=49269r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=49269r=float No Zend Extensions: http://bugs.php.net/fix.php?id=49269r=nozend MySQL Configuration Error: http://bugs.php.net/fix.php?id=49269r=mysqlcfg
#49269 [Opn]: isset() fails on Iterator object when used inside foreach declaration
ID: 49269 User updated by: president at basnetworks dot net Reported By: president at basnetworks dot net Status: Open Bug Type: Class/Object related Operating System: All PHP Version: 5.3.0 New Comment: I addition to the reproduce code, the following may help to understand the bug: foreach ($array_object as $item) { echo $item; } Will successfully print abc, while: foreach ((isset($array_object) ? $array_object : array()) as $item) { echo $item; } will not print anything, indicating that isset() is returning false. I hope that helps. Previous Comments: [2009-08-16 01:20:32] president at basnetworks dot net Description: The function isset() produces an incorrect result when used on an object that implements the Iterator interface, within a foreach declaration. As illustrated below, when used outside of the foreach() declaration, isset() works as expected in the object that implements Iterator interface, returning true, causing the output 'true'. When isset() is used within the foreach() declaration on the same object, it instead returns false, causing an empty array to be used for the foreach() loop. There is no reason the isset() function should return anything different when used within the foreach() declaration block. Reproduce code: --- class NormalClass { public $a, $b, $c; public function __construct() { $this-a = 'a'; $this-b = 'b'; $this-c = 'c'; } } class ArrayClass implements Iterator { private $internal_array; public function __construct() { $this-internal_array = array('a', 'b', 'c'); } public function key() { return key($this-nodes); } public function current() { return current($this-nodes); } public function next() { next($this-nodes); } public function valid() { return (current($this-nodes) !== false) ? true : false; } public function rewind() { reset($this-nodes); } } $array = array('a', 'b', 'c'); $normal_object = new NormalClass(); $array_object = new ArrayClass(); echo Array: . (isset($array) ? 'true' : 'false'); echo \nNormal Object: . (isset($normal_object) ? 'true' : 'false'); echo \nArray Object: . (isset($array_object) ? 'true' : 'false'); echo \nArray: ; foreach ((isset($array) ? $array : array()) as $item) { echo $item; } echo \nNormal Object: ; foreach ((isset($normal_object) ? $normal_object : array()) as $item) { echo $item; } echo \nArray Object: ; foreach ((isset($array_object) ? $array_object : array()) as $item) { echo $item; } Expected result: Array: true Normal Object: true Array Object: true Array: abc Normal Object: abc Array Object: abc Actual result: -- Array: true Normal Object: true Array Object: true Array: abc Normal Object: abc Array Object: -- Edit this bug report at http://bugs.php.net/?id=49269edit=1
#49269 [Opn]: isset() fails on Iterator object when used inside foreach declaration
ID: 49269 User updated by: president at basnetworks dot net Reported By: president at basnetworks dot net Status: Open Bug Type: Class/Object related Operating System: All PHP Version: 5.3.0 New Comment: After further testing, I have found this bug is stranger than it seems: foreach ((isset($array_object) ? $array_object : array('1', '2', '3')) as $item) { echo $item; } Should either print 'abc' or '123' no matter if isset() is successful or fails. It prints neither. Now I am wondering if it is not isset(), but the ternary operator that is failing. Previous Comments: [2009-08-16 01:28:52] president at basnetworks dot net I addition to the reproduce code, the following may help to understand the bug: foreach ($array_object as $item) { echo $item; } Will successfully print abc, while: foreach ((isset($array_object) ? $array_object : array()) as $item) { echo $item; } will not print anything, indicating that isset() is returning false. I hope that helps. [2009-08-16 01:20:32] president at basnetworks dot net Description: The function isset() produces an incorrect result when used on an object that implements the Iterator interface, within a foreach declaration. As illustrated below, when used outside of the foreach() declaration, isset() works as expected in the object that implements Iterator interface, returning true, causing the output 'true'. When isset() is used within the foreach() declaration on the same object, it instead returns false, causing an empty array to be used for the foreach() loop. There is no reason the isset() function should return anything different when used within the foreach() declaration block. Reproduce code: --- class NormalClass { public $a, $b, $c; public function __construct() { $this-a = 'a'; $this-b = 'b'; $this-c = 'c'; } } class ArrayClass implements Iterator { private $internal_array; public function __construct() { $this-internal_array = array('a', 'b', 'c'); } public function key() { return key($this-nodes); } public function current() { return current($this-nodes); } public function next() { next($this-nodes); } public function valid() { return (current($this-nodes) !== false) ? true : false; } public function rewind() { reset($this-nodes); } } $array = array('a', 'b', 'c'); $normal_object = new NormalClass(); $array_object = new ArrayClass(); echo Array: . (isset($array) ? 'true' : 'false'); echo \nNormal Object: . (isset($normal_object) ? 'true' : 'false'); echo \nArray Object: . (isset($array_object) ? 'true' : 'false'); echo \nArray: ; foreach ((isset($array) ? $array : array()) as $item) { echo $item; } echo \nNormal Object: ; foreach ((isset($normal_object) ? $normal_object : array()) as $item) { echo $item; } echo \nArray Object: ; foreach ((isset($array_object) ? $array_object : array()) as $item) { echo $item; } Expected result: Array: true Normal Object: true Array Object: true Array: abc Normal Object: abc Array Object: abc Actual result: -- Array: true Normal Object: true Array Object: true Array: abc Normal Object: abc Array Object: -- Edit this bug report at http://bugs.php.net/?id=49269edit=1