From: Alexander Schunk on
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
From: Richard Quadling on
On 4 May 2010 14:02, Alexander Schunk <aschunk(a)gmail.com> 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;
>           }
>      }
>
> 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
>
>

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

--
-----
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=ZEND002498&r=213474731
ZOPA : http://uk.zopa.com/member/RQuadling
From: Ferenc Kovacs on
On Tue, May 4, 2010 at 3:02 PM, Alexander Schunk <aschunk(a)gmail.com> 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
From: "Sascha Meyer" on
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