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 #180737 is a reply to message #180736] Fri, 15 March 2013 17:53 Go to previous messageGo to previous message
daveh is currently offline  daveh
Messages: 18
Registered: March 2013
Karma:
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
[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: Thu Nov 28 03:39:17 GMT 2024

Total time taken to generate the page: 0.04090 seconds