/    Sign up×
Community /Pin to ProfileBookmark

OOP example ?

Hi php masters,

please show me one of OO example.

I can’t succeed with the following one.
I get error.

[code=php]

class human {
private $meal;
function eat() {
return $this->meal;
}
}

[/code]

Many thanks in advance.

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@sridhar_423May 05.2006 — var $meal;

i'm using 4.1.2 ver.
Copy linkTweet thisAlerts:
@SpectreReturnsMay 05.2006 — What error do you get?
Copy linkTweet thisAlerts:
@orreipMay 05.2006 — Private limits visibility only to the class that defines the item. Read the manual about members visibility. The meal variable is null as you defined your class.

i'm guessing that you're trying smt. like :

$h=new Human;

$h->meal="ham"; //you cannot assign this outside of your class

echo $h->eat();

you can use:

public $meal;

or define another method in your class:

public function setMeal($string)

{

$this->meal=$string;

}

and use $h->setMeal("ham");

This is only if you're using PHP5. The other ones doesn't support private, public, protected.
Copy linkTweet thisAlerts:
@NogDogMay 05.2006 — Let us know if you're using PHP4 or PHP5, as PHP4 does not support as much object-oriented functionality as PHP5 does (such as the "private" declaration).
×

Success!

Help @iota spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...