Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #8778

Re: question about class getters

From Jerry Stuckle <jstucklex@attglobal.net>
Newsgroups comp.lang.php
Subject Re: question about class getters
Date 2013-03-21 08:55 -0400
Organization A noiseless patient Spider
Message-ID <kievso$9hu$1@dont-email.me> (permalink)
References <acaaecf6-a484-4603-8c21-6dad66988e18@c6g2000yqh.googlegroups.com> <khvkl7$c5m$1@dont-email.me> <d4b5e6d8-c4c5-4aca-a857-71404fe8df5c@7g2000yqy.googlegroups.com> <khvqpp$h7g$1@dont-email.me> <kiek0l$vq1$1@dont-email.me>

Show all headers | View raw


On 3/21/2013 5:31 AM, Curtis Dyer wrote:
> Jerry Stuckle <jstucklex@attglobal.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@attglobal.net
==================

Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

question about class getters David Heller <daveh@allheller.net> - 2013-03-15 08:42 -0700
  Re: question about class getters Salvatore <sal@yojimbo.hack.invalid> - 2013-03-15 16:12 +0000
    Re: question about class getters David Heller <daveh@allheller.net> - 2013-03-15 09:48 -0700
      Re: question about class getters "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2013-03-16 12:06 +0100
  Re: question about class getters Jerry Stuckle <jstucklex@attglobal.net> - 2013-03-15 13:11 -0400
    Re: question about class getters David Heller <daveh@allheller.net> - 2013-03-15 10:53 -0700
      Re: question about class getters Jerry Stuckle <jstucklex@attglobal.net> - 2013-03-15 14:56 -0400
        Re: question about class getters Curtis Dyer <dyer85@gmail.com> - 2013-03-21 09:31 +0000
          Re: question about class getters Jerry Stuckle <jstucklex@attglobal.net> - 2013-03-21 08:55 -0400
            Re: question about class getters Christoph Becker <cmbecker69@gmx.de> - 2013-03-21 14:12 +0100
            Re: question about class getters Curtis Dyer <dyer85@gmail.com> - 2013-03-23 07:14 +0000
              Re: question about class getters Jerry Stuckle <jstucklex@attglobal.net> - 2013-03-23 08:46 -0400

csiph-web