FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » question about class getters
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: question about class getters [message #180735 is a reply to message #180734] Fri, 15 March 2013 16:48 Go to previous messageGo to previous message
daveh is currently offline  daveh
Messages: 18
Registered: March 2013
Karma:
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
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Will this set or get a SESSION variable?
Next Topic: Fatal error!
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Tue Nov 26 04:25:05 GMT 2024

Total time taken to generate the page: 0.04003 seconds