question about class getters [message #180733] |
Fri, 15 March 2013 15:42 |
daveh
Messages: 18 Registered: March 2013
Karma: 0
|
Junior Member |
|
|
Hello
I have the following code snippet:
Class myClass
{
public function makeArray()
{
$this->my_array = array('Mon', 'Tues', 'Wed', 'Thurs', 'Friday',
'Sat', 'Sun');
}
pubic function getArray()
{
return $this->my_array;
}
}
and then do the following
var $MyVar = new myClass();
var $mydate = $MyVar->getArray[1];
echo $mydate;
=> Mon
will this work? If not how to make it work?
Thanks,
Dave
|
|
|
Re: question about class getters [message #180734 is a reply to message #180733] |
Fri, 15 March 2013 16:12 |
Salvatore
Messages: 38 Registered: September 2012
Karma: 0
|
Member |
|
|
On 2013-03-15, David Heller <daveh(at)allheller(dot)net> wrote:
> var $MyVar = new myClass();
> var $mydate = $MyVar->getArray[1];
> echo $mydate;
> => Mon
> will this work? If not how to make it work?
Ah, I see now. Well, to answer your first question, it won't work. The
type of $MyVar->getArray is function (or callback), not array.
The proper way of writing a getter in PHP is like so:
<?php
class myClass {
private $_array = array();
public function __get($name) {
switch ($name) {
case 'array':
break;
}
}
}
?>
In this example, when you fetch "$MyVar->array" you will get the array
by value and not by reference, so the following code will work...
<?php
$MyVar = new myClass();
$mydate = $MyVar->array[1];
?>
....but the following code won't...
<?php
$MyVar = new myClass();
$mydate = 'Mon';
$MyVar->array[1] = $mydate;
?>
....because the property $_array will never be manipulated.
Do you want to be able to manipulate the array in the object, or just
return the array?
--
Blah blah bleh...
GCS/CM d(-)@>-- s+:- !a C++$ UBL++++$ L+$ W+++$ w M++ Y++ b++
|
|
|
Re: question about class getters [message #180735 is a reply to message #180734] |
Fri, 15 March 2013 16:48 |
daveh
Messages: 18 Registered: March 2013
Karma: 0
|
Junior Member |
|
|
On Mar 15, 12:12 pm, Salvatore <s...@yojimbo.hack.invalid> wrote:
> On 2013-03-15, David Heller <da...@allheller.net> wrote:
>
>> var $MyVar = new myClass();
>> var $mydate = $MyVar->getArray[1];
>> echo $mydate;
>> => Mon
>> will this work? If not how to make it work?
>
> Ah, I see now. Well, to answer your first question, it won't work. The
> type of $MyVar->getArray is function (or callback), not array.
>
> The proper way of writing a getter in PHP is like so:
>
> <?php
> class myClass {
> private $_array = array();
>
> public function __get($name) {
> switch ($name) {
> case 'array':
> break;
> }
> }}
>
> ?>
>
> In this example, when you fetch "$MyVar->array" you will get the array
> by value and not by reference, so the following code will work...
>
> <?php
> $MyVar = new myClass();
> $mydate = $MyVar->array[1];
> ?>
>
> ...but the following code won't...
>
> <?php
> $MyVar = new myClass();
> $mydate = 'Mon';
> $MyVar->array[1] = $mydate;
> ?>
>
> ...because the property $_array will never be manipulated.
>
> Do you want to be able to manipulate the array in the object, or just
> return the array?
>
> --
> Blah blah bleh...
> GCS/CM d(-)@>-- s+:- !a C++$ UBL++++$ L+$ W+++$ w M++ Y++ b++
Thanks
I don't need to manipulate the array in the object just get its value.
I am trying to update some php 4.0.6 code that makes liberal use of
the Super Global "$GLOBALS" and eval() (perhaps there was no other way
in php 4.0.6) which I have updated to work with php 5.3 but would like
to rewrite the code completely to not make use of such an unsafe
method of passing variables around! Using a getter method seems to be
the best or better alternative.
Dave
|
|
|
Re: question about class getters [message #180736 is a reply to message #180733] |
Fri, 15 March 2013 17:11 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On 3/15/2013 11:42 AM, David Heller wrote:
>
> Hello
>
> I have the following code snippet:
>
> Class myClass
>
> {
> public function makeArray()
> {
> $this->my_array = array('Mon', 'Tues', 'Wed', 'Thurs', 'Friday',
> 'Sat', 'Sun');
> }
> pubic function getArray()
> {
> return $this->my_array;
> }
> }
>
> and then do the following
>
> var $MyVar = new myClass();
> var $mydate = $MyVar->getArray[1];
> echo $mydate;
> => Mon
> will this work? If not how to make it work?
>
> Thanks,
>
> Dave
>
Not quite, for a couple of reasons. As Salvatore pointed out,
getArray() is a function, not a variable, so you need to code it as a
function. The other problem is you never called makeArray() and don't
have a constructor, so $my_array doesn't exist (also it should be
declared as a class variable).
Something like this should works:
class MyClass {
private $my_array;
public function __construct() { // Constructor
$this->my_array = array('Mon', 'Tues', 'Wed', 'Thurs', 'Friday',
'Sat', 'Sun');
}
public function getArray($i) {
if ($i >= 0 && $i < count($this->my_array)) // Verify index
return $this->my_array[$i];
else
return null; // Out of range
}
}
$myVar = new MyClass();
$mydate = $myVar->getArray(1); // Pass index as an argument
echo $mydate;
=> Tues
(PHP array indexes start with zero, not one).
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
|
|
|
Re: question about class getters [message #180737 is a reply to message #180736] |
Fri, 15 March 2013 17:53 |
daveh
Messages: 18 Registered: March 2013
Karma: 0
|
Junior Member |
|
|
On Mar 15, 1:11 pm, Jerry Stuckle <jstuck...@attglobal.net> wrote:
> On 3/15/2013 11:42 AM, David Heller wrote:
>
>
>
>
>
>
>
>
>
>
>
>> Hello
>
>> I have the following code snippet:
>
>> Class myClass
>
>> {
>> public function makeArray()
>> {
>> $this->my_array = array('Mon', 'Tues', 'Wed', 'Thurs', 'Friday',
>> 'Sat', 'Sun');
>> }
>> pubic function getArray()
>> {
>> return $this->my_array;
>> }
>> }
>
>> and then do the following
>
>> var $MyVar = new myClass();
>> var $mydate = $MyVar->getArray[1];
>> echo $mydate;
>> => Mon
>> will this work? If not how to make it work?
>
>> Thanks,
>
>> Dave
>
> Not quite, for a couple of reasons. As Salvatore pointed out,
> getArray() is a function, not a variable, so you need to code it as a
> function. The other problem is you never called makeArray() and don't
> have a constructor, so $my_array doesn't exist (also it should be
> declared as a class variable).
>
> Something like this should works:
>
> class MyClass {
> private $my_array;
>
> public function __construct() { // Constructor
> $this->my_array = array('Mon', 'Tues', 'Wed', 'Thurs', 'Friday',
> 'Sat', 'Sun');
> }
> public function getArray($i) {
> if ($i >= 0 && $i < count($this->my_array)) // Verify index
> return $this->my_array[$i];
> else
> return null; // Out of range
> }
>
> }
>
> $myVar = new MyClass();
> $mydate = $myVar->getArray(1); // Pass index as an argument
> echo $mydate;
>
> => Tues
>
> (PHP array indexes start with zero, not one).
>
> --
> ==================
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstuck...@attglobal.net
> ==================
Even better:
class MyClass {
private $my_array;
public function __construct() { // Constructor
$this->my_array = array('Mon', 'Tues', 'Wed', 'Thurs', 'Friday',
'Sat', 'Sun');
}
public function __get($i) {
// if ($i >= 0 && $i < count($this->my_array)) // Verify index
if(array_key_exists($i, $this->my_array)){ // For keys
that are non-numerical
return $this->my_array[$i];}
else
return null; // Out of range or non-existant
}
}
I get it now (no pun intended).
Dave
|
|
|
Re: question about class getters [message #180738 is a reply to message #180737] |
Fri, 15 March 2013 18:56 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On 3/15/2013 1:53 PM, David Heller wrote:
> On Mar 15, 1:11 pm, Jerry Stuckle <jstuck...@attglobal.net> wrote:
>> On 3/15/2013 11:42 AM, David Heller wrote:
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>> Hello
>>
>>> I have the following code snippet:
>>
>>> Class myClass
>>
>>> {
>>> public function makeArray()
>>> {
>>> $this->my_array = array('Mon', 'Tues', 'Wed', 'Thurs', 'Friday',
>>> 'Sat', 'Sun');
>>> }
>>> pubic function getArray()
>>> {
>>> return $this->my_array;
>>> }
>>> }
>>
>>> and then do the following
>>
>>> var $MyVar = new myClass();
>>> var $mydate = $MyVar->getArray[1];
>>> echo $mydate;
>>> => Mon
>>> will this work? If not how to make it work?
>>
>>> Thanks,
>>
>>> Dave
>>
>> Not quite, for a couple of reasons. As Salvatore pointed out,
>> getArray() is a function, not a variable, so you need to code it as a
>> function. The other problem is you never called makeArray() and don't
>> have a constructor, so $my_array doesn't exist (also it should be
>> declared as a class variable).
>>
>> Something like this should works:
>>
>> class MyClass {
>> private $my_array;
>>
>> public function __construct() { // Constructor
>> $this->my_array = array('Mon', 'Tues', 'Wed', 'Thurs', 'Friday',
>> 'Sat', 'Sun');
>> }
>> public function getArray($i) {
>> if ($i >= 0 && $i < count($this->my_array)) // Verify index
>> return $this->my_array[$i];
>> else
>> return null; // Out of range
>> }
>>
>> }
>>
>> $myVar = new MyClass();
>> $mydate = $myVar->getArray(1); // Pass index as an argument
>> echo $mydate;
>>
>> => Tues
>>
>> (PHP array indexes start with zero, not one).
>>
>> --
>> ==================
>> Remove the "x" from my email address
>> Jerry Stuckle
>> JDS Computer Training Corp.
>> jstuck...@attglobal.net
>> ==================
>
> Even better:
>
> class MyClass {
> private $my_array;
>
> public function __construct() { // Constructor
> $this->my_array = array('Mon', 'Tues', 'Wed', 'Thurs', 'Friday',
> 'Sat', 'Sun');
> }
> public function __get($i) {
> // if ($i >= 0 && $i < count($this->my_array)) // Verify index
> if(array_key_exists($i, $this->my_array)){ // For keys
> that are non-numerical
> return $this->my_array[$i];}
> else
> return null; // Out of range or non-existant
> }
>
> }
>
> I get it now (no pun intended).
>
> Dave
>
No, __get is a "magic method" and used to access properties for which
you have no special getter method. You need to pass the name of the
property you wish. It really doesn't conform to OO standards, and as
your object complexity increases, the complexity of your magic methods
increases. It's also not applicable to passing additional parameters,
such as the index you wish.
I really prefer to use individual getter and setter methods for object
properties, as is indicated in OO principles.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
|
|
|
Re: question about class getters [message #180741 is a reply to message #180735] |
Sat, 16 March 2013 11:06 |
M. Strobel
Messages: 386 Registered: December 2011
Karma: 0
|
Senior Member |
|
|
Am 15.03.2013 17:48, schrieb David Heller:
---------
> I don't need to manipulate the array in the object just get its value.
> I am trying to update some php 4.0.6 code that makes liberal use of
> the Super Global "$GLOBALS" and eval() (perhaps there was no other way
> in php 4.0.6) which I have updated to work with php 5.3 but would like
> to rewrite the code completely to not make use of such an unsafe
> method of passing variables around! Using a getter method seems to be
> the best or better alternative.
>
> Dave
>
This is an example of what one may call straight coding - just plain features used.
<?php
class Dayofweek {
# constant init values okay
private $a = array('Mon', 'Tues', 'Wed', 'Thurs', 'Friday','Sat', 'Sun');
function getDow($n) {
if ($this->isInbounds($n)) {
return $this->a[$n];
} else {
trigger_error('Dayofweek index out of range', E_USER_ERROR);
}
}
function isInbounds($n) {
return (-1 < $n && $n < count($this->a));
}
function getDow1based($n) {
return $this->getDow($n-1);
}
function getDowTolerant($n) {
if ($this->isInbounds($n)) return $this->a[$n];
else return "";
}
}
/Str.
|
|
|
Re: question about class getters [message #180839 is a reply to message #180738] |
Thu, 21 March 2013 09:31 |
Curtis Dyer
Messages: 34 Registered: January 2011
Karma: 0
|
Member |
|
|
Jerry Stuckle <jstucklex(at)attglobal(dot)net> wrote:
> On 3/15/2013 1:53 PM, David Heller wrote:
>> On Mar 15, 1:11 pm, Jerry Stuckle <jstuck...@attglobal.net>
>> wrote:
>>> On 3/15/2013 11:42 AM, David Heller wrote:
>>>
>>>> Hello
>>>
>>>> I have the following code snippet:
>>>
>>>> Class myClass
>>>
>>>> {
>>>> public function makeArray()
>>>> {
>>>> $this->my_array = array('Mon', 'Tues', 'Wed', 'Thurs',
>>>> 'Friday', 'Sat', 'Sun');
>>>> }
>>>> pubic function getArray()
>>>> {
>>>> return $this->my_array;
>>>> }
>>>> }
>>>
>>>> and then do the following
>>>
>>>> var $MyVar = new myClass();
>>>> var $mydate = $MyVar->getArray[1];
>>>> echo $mydate;
>>>> => Mon
>>>> will this work? If not how to make it work?
Why not try first and see what happens?
<snip>
>>> Not quite, for a couple of reasons. As Salvatore pointed out,
>>> getArray() is a function, not a variable, so you need to code
>>> it as a function. The other problem is you never called
>>> makeArray() and don't have a constructor, so $my_array doesn't
>>> exist (also it should be declared as a class variable).
>>>
>>> Something like this should works:
<snip code>
>> Even better:
>>
>> class MyClass {
>> private $my_array;
>>
>> public function __construct() { // Constructor
>> $this->my_array = array('Mon', 'Tues', 'Wed', 'Thurs',
>> 'Friday',
>> 'Sat', 'Sun');
Just out of curiosity, why is `Friday' the only day of the week
that's not abbreviated in your list?
>> }
>> public function __get($i) {
>> // if ($i >= 0 && $i < count($this->my_array)) //
>> Verify index if(array_key_exists($i, $this->my_array)){
>> // For keys
>> that are non-numerical
>> return $this->my_array[$i];}
>> else
>> return null; // Out of range or non-existant
>> }
>>
>> }
>>
>> I get it now (no pun intended).
>
> No, __get is a "magic method" and used to access properties for
> which you have no special getter method. You need to pass the
> name of the property you wish.
Magic methods like `__get()' are called when "inaccessible"
properties or methods are invoked, at which point you may handle
the call any way you wish. According to the PHP manual[1],
"inaccessible" methods and properties include those which are not
defined, so you don't need to specify the names of actual class
members for `__get()' to work.
[1]: <http://php.net/__get>
<snip>
> It's also not
> applicable to passing additional parameters, such as the index
> you wish.
Yes, the `__get()' magic method expects a string, and moreover, if
the OP intends to use the class similarly to the code they posted
initially, it would be a syntax error:
echo $obj->2; /* parse error */
Using a custom getter method as you posted above, IMO, seems
easiest.
--
Curtis Dyer
<?$x='<?$x=%c%s%c;printf($x,39,$x,39);?>';printf($x,39,$x,39);?>
|
|
|
Re: question about class getters [message #180842 is a reply to message #180839] |
Thu, 21 March 2013 12:55 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On 3/21/2013 5:31 AM, Curtis Dyer wrote:
> Jerry Stuckle <jstucklex(at)attglobal(dot)net> wrote:
>
>> On 3/15/2013 1:53 PM, David Heller wrote:
>>> On Mar 15, 1:11 pm, Jerry Stuckle <jstuck...@attglobal.net>
>>> wrote:
>>>> On 3/15/2013 11:42 AM, David Heller wrote:
>>>>
>>>> > Hello
>>>>
>>>> > I have the following code snippet:
>>>>
>>>> > Class myClass
>>>>
>>>> > {
>>>> > public function makeArray()
>>>> > {
>>>> > $this->my_array = array('Mon', 'Tues', 'Wed', 'Thurs',
>>>> > 'Friday', 'Sat', 'Sun');
>>>> > }
>>>> > pubic function getArray()
>>>> > {
>>>> > return $this->my_array;
>>>> > }
>>>> > }
>>>>
>>>> > and then do the following
>>>>
>>>> > var $MyVar = new myClass();
>>>> > var $mydate = $MyVar->getArray[1];
>>>> > echo $mydate;
>>>> > => Mon
>>>> > will this work? If not how to make it work?
>
> Why not try first and see what happens?
>
> <snip>
>
>>>> Not quite, for a couple of reasons. As Salvatore pointed out,
>>>> getArray() is a function, not a variable, so you need to code
>>>> it as a function. The other problem is you never called
>>>> makeArray() and don't have a constructor, so $my_array doesn't
>>>> exist (also it should be declared as a class variable).
>>>>
>>>> Something like this should works:
>
> <snip code>
>
>>> Even better:
>>>
>>> class MyClass {
>>> private $my_array;
>>>
>>> public function __construct() { // Constructor
>>> $this->my_array = array('Mon', 'Tues', 'Wed', 'Thurs',
>>> 'Friday',
>>> 'Sat', 'Sun');
>
> Just out of curiosity, why is `Friday' the only day of the week
> that's not abbreviated in your list?
>
It's not my list.
>>> }
>>> public function __get($i) {
>>> // if ($i >= 0 && $i < count($this->my_array)) //
>>> Verify index if(array_key_exists($i, $this->my_array)){
>>> // For keys
>>> that are non-numerical
>>> return $this->my_array[$i];}
>>> else
>>> return null; // Out of range or non-existant
>>> }
>>>
>>> }
>>>
>>> I get it now (no pun intended).
>>
>> No, __get is a "magic method" and used to access properties for
>> which you have no special getter method. You need to pass the
>> name of the property you wish.
>
> Magic methods like `__get()' are called when "inaccessible"
> properties or methods are invoked, at which point you may handle
> the call any way you wish. According to the PHP manual[1],
> "inaccessible" methods and properties include those which are not
> defined, so you don't need to specify the names of actual class
> members for `__get()' to work.
>
Yes, I know what magic methods are. But that doesn't mean you can't
call the method directly.
>
> [1]: <http://php.net/__get>
>
> <snip>
>
>> It's also not
>> applicable to passing additional parameters, such as the index
>> you wish.
>
> Yes, the `__get()' magic method expects a string, and moreover, if
> the OP intends to use the class similarly to the code they posted
> initially, it would be a syntax error:
>
> echo $obj->2; /* parse error */
>
> Using a custom getter method as you posted above, IMO, seems
> easiest.
>
echo $obj->'2'; should work (although I haven't tried it).
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
|
|
|
Re: question about class getters [message #180844 is a reply to message #180842] |
Thu, 21 March 2013 13:12 |
Christoph Becker
Messages: 91 Registered: June 2012
Karma: 0
|
Member |
|
|
Jerry Stuckle wrote:
> echo $obj->'2'; should work (although I haven't tried it).
That is a syntactic error. FWIW the following works:
PHP 5.4.7 (cli) (???) [WINNT]
>>> class Test {function __get($i) {return $i;}}
>>> $obj = new Test
>>> $obj->{2}
>>> echo $obj->{2}
2
However, I wouldn't use it.
--
Christoph M. Becker
|
|
|
Re: question about class getters [message #180870 is a reply to message #180842] |
Sat, 23 March 2013 07:14 |
Curtis Dyer
Messages: 34 Registered: January 2011
Karma: 0
|
Member |
|
|
Jerry Stuckle <jstucklex(at)attglobal(dot)net> wrote:
> On 3/21/2013 5:31 AM, Curtis Dyer wrote:
>> Jerry Stuckle <jstucklex(at)attglobal(dot)net> wrote:
>>
>>> On 3/15/2013 1:53 PM, David Heller wrote:
>>>> On Mar 15, 1:11 pm, Jerry Stuckle <jstuck...@attglobal.net>
>>>> wrote:
>>>> > On 3/15/2013 11:42 AM, David Heller wrote:
<snip>
>>>> public function __construct() { // Constructor
>>>> $this->my_array = array('Mon', 'Tues', 'Wed',
>>>> 'Thurs', 'Friday',
>>>> 'Sat', 'Sun');
>>
>> Just out of curiosity, why is `Friday' the only day of the week
>> that's not abbreviated in your list?
>
> It's not my list.
Right, but this portion of my response was to the OP. If I botched
my quoting, my apologies.
<snip>
>> Yes, the `__get()' magic method expects a string, and moreover,
>> if the OP intends to use the class similarly to the code they
>> posted initially, it would be a syntax error:
>>
>> echo $obj->2; /* parse error */
>>
>> Using a custom getter method as you posted above, IMO, seems
>> easiest.
>
> echo $obj->'2'; should work (although I haven't tried it).
That's still a syntax error; I tested in 5.4.12. However, I haven't
bothered to check if it works in prior versions.
--
Curtis Dyer
<?$x='<?$x=%c%s%c;printf($x,39,$x,39);?>';printf($x,39,$x,39);?>
|
|
|
Re: question about class getters [message #180871 is a reply to message #180870] |
Sat, 23 March 2013 12:46 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On 3/23/2013 3:14 AM, Curtis Dyer wrote:
> Jerry Stuckle <jstucklex(at)attglobal(dot)net> wrote:
>
>> On 3/21/2013 5:31 AM, Curtis Dyer wrote:
>>> Jerry Stuckle <jstucklex(at)attglobal(dot)net> wrote:
>>>
>>>> On 3/15/2013 1:53 PM, David Heller wrote:
>>>> > On Mar 15, 1:11 pm, Jerry Stuckle <jstuck...@attglobal.net>
>>>> > wrote:
>>>> >> On 3/15/2013 11:42 AM, David Heller wrote:
>
> <snip>
>
>>>> > public function __construct() { // Constructor
>>>> > $this->my_array = array('Mon', 'Tues', 'Wed',
>>>> > 'Thurs', 'Friday',
>>>> > 'Sat', 'Sun');
>>>
>>> Just out of curiosity, why is `Friday' the only day of the week
>>> that's not abbreviated in your list?
>>
>> It's not my list.
>
> Right, but this portion of my response was to the OP. If I botched
> my quoting, my apologies.
>
> <snip>
>
>>> Yes, the `__get()' magic method expects a string, and moreover,
>>> if the OP intends to use the class similarly to the code they
>>> posted initially, it would be a syntax error:
>>>
>>> echo $obj->2; /* parse error */
>>>
>>> Using a custom getter method as you posted above, IMO, seems
>>> easiest.
>>
>> echo $obj->'2'; should work (although I haven't tried it).
>
> That's still a syntax error; I tested in 5.4.12. However, I haven't
> bothered to check if it works in prior versions.
>
As I said - it was untested.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
|
|
|