Re: how to access class members

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Alex,

Alex wrote:
> How to access class members?
>
> I have the following class definition:
>
as Richard pointed out, you were not pointing at the class member's name but to the visibility of the variable instead; to give you a start, this is how your class should look like (but I would really advise to have a look at Richard's link to get a better understanding of class usage and OOP):

[CODE]
class Mitarbeiter{
    // use private if you are using getter methods to access members
    private $name = 'meier';

    public $alter = '50';
    public $gehalt = '2000';
    public $abteilung = 'programmierung';

    function get_name (){
        return $this->name;
    }

    function printMember(){
        echo 'Name: ' . $this->name . "<br />;
        echo 'Alter: ' . $this->alter . "<br />";
        echo 'Gehalt: ' . $this->gehalt . "<br />;
        echo 'Abteilung: ' . $this->abteilung . "<br />;
    }
}

$m = new Mitarbeiter();
$m->printMember();

// this would fail, because I changed the visibility of member 'name' from
// public to private
print $m->name;
// use a getter method instead:
print $m->get_name();

// this would work because the visibility is public, member is accessible from outside of the class
print $m->alter;

[/CODE]

Regards,

Sascha

--------------------------------------------------
EE:  http://www.experts-exchange.com/M_761556.html
ZCE: http://www.zend.com/en/yellow-pages#show-ClientCandidateID=ZEND011290
-- 
GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT!
Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01

-- 
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [PHP Database Programming]     [PHP Install]     [Kernel Newbies]     [Yosemite Forum]     [PHP Books]

  Powered by Linux