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 #180734 is a reply to message #180733] Fri, 15 March 2013 16:12 Go to previous messageGo to previous message
Salvatore is currently offline  Salvatore
Messages: 38
Registered: September 2012
Karma:
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++
[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: Sat Nov 30 21:19:13 GMT 2024

Total time taken to generate the page: 0.03753 seconds