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


Groups > comp.lang.php > #8670

Re: question about class getters

From Salvatore <sal@yojimbo.hack.invalid>
Newsgroups comp.lang.php
Subject Re: question about class getters
Date 2013-03-15 16:12 +0000
Organization Yojimbo NNTP server
Message-ID <khvh8s$13k6$1@yojimbo.hack> (permalink)
References <acaaecf6-a484-4603-8c21-6dad66988e18@c6g2000yqh.googlegroups.com>

Show all headers | View raw


On 2013-03-15, David Heller <daveh@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++

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