how to access class members

how to access class members

am 04.05.2010 15:02:24 von Alexander Schunk

Hello,

i have a question:

How to access class members?

I have the following class definition:

class Mitarbeiter{

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

function printMember(){
echo 'Name: ' . $this->public;
echo 'Alter: ' . $this->public;
echo 'Gehalt: ' . $this->public;
echo 'Abteilung: ' . $this->public;
}
}

Now i want to access the public member with:

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

When calling method printMember() i get an error "cannot access empty
class member".

I dont see why the class members are empty?

Regards
Alexander

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

Re: how to access class members

am 04.05.2010 15:20:15 von Richard Quadling

On 4 May 2010 14:02, Alexander Schunk wrote:
> Hello,
>
> i have a question:
>
> How to access class members?
>
> I have the following class definition:
>
> class Mitarbeiter{
>
>           public $name =3D 'meier';
>           public $alter =3D '50';
>           public $gehalt =3D '2000';
>           public $abteilung =3D 'programmierung'=
;
>
>           function printMember(){
>              echo 'Name: ' . $this->pu=
blic;
>              echo 'Alter: ' . $this->p=
ublic;
>              echo 'Gehalt: ' . $this->=
public;
>              echo 'Abteilung: ' . $thi=
s->public;
>           }
>      }
>
> Now i want to access the public member with:
>
> $m =3D new Mitarbeiter();
> $m->printMember();
>
> When calling method printMember() i get an error "cannot access empty
> class member".
>
> I dont see why the class members are empty?
>
> Regards
> Alexander
>
> --
> PHP Windows Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

echo $this->name;

public relates to the visibility of the property (public - can be seen
by all, protected - can only be seen by this class and sub-classes and
private - can only be seen by this class).

Read more about this at
http://docs.php.net/manual/en/language.oop5.visibility.php

--=20
-----
Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE : http://www.experts-exchange.com/M_248814.html
EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
Zend Certified Engineer : http://zend.com/zce.php?c=3DZEND002498&r=3D213474=
731
ZOPA : http://uk.zopa.com/member/RQuadling

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

Re: how to access class members

am 04.05.2010 15:44:21 von Ferenc Kovacs

--0015174c11182a84190485c4e819
Content-Type: text/plain; charset=UTF-8

On Tue, May 4, 2010 at 3:02 PM, Alexander Schunk wrote:

> Hello,
>
> i have a question:
>
> How to access class members?
>
> I have the following class definition:
>
> class Mitarbeiter{
>
> public $name = 'meier';
> public $alter = '50';
> public $gehalt = '2000';
> public $abteilung = 'programmierung';
>
> function printMember(){
> echo 'Name: ' . $this->public;
> echo 'Alter: ' . $this->public;
> echo 'Gehalt: ' . $this->public;
> echo 'Abteilung: ' . $this->public;
> }
>

this should be:
function printMember(){
echo 'Name: ' . $this->name;
echo 'Alter: ' . $this->alter;
echo 'Gehalt: ' . $this->gehalt;
echo 'Abteilung: ' . $this->abteilung;
}

Tyrael

--0015174c11182a84190485c4e819--

Re: how to access class members

am 04.05.2010 15:49:12 von harlequin2

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 . "
;
echo 'Alter: ' . $this->alter . "
";
echo 'Gehalt: ' . $this->gehalt . "
;
echo 'Abteilung: ' . $this->abteilung . "
;
}
}

$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=Z END011290
--
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