/    Sign up×
Community /Pin to ProfileBookmark

__get and __set method

hi all
i have problem how to use __get and __set methods in PHP
i have diffiulty to understand this magic methods
I need definitions of this methods in a broad range
Thanks beforehands

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@criterion9Feb 04.2010 — Did you try the manual?
Copy linkTweet thisAlerts:
@azegurbauthorFeb 05.2010 — thanks but i dont understand

can you explain me one example step by step so i understand

thank you for attention
Copy linkTweet thisAlerts:
@NogDogFeb 05.2010 — [code=php]
<?php
class Foo
{
private $bar = 0; // cannot normally be accessed outside of the class
public function __set($var, $value)
{
if ($var == 'bar') {
// force value to be integer
$this->$var = (int)$value;
} else {
throw new Exception("Invalid varname '$var'");
}
}
public function __get($var)
{
if ($var == 'bar') {
return $this->$var;
}
throw new Exception("Invalid varname '$var'");
}
}
try {
$test = new Foo();
$test->bar = 99;
echo $test->bar; // 99
echo "<br />n";
$test->bar = '123.456';
echo $test->bar; // 123
$test->xxx = 333; // throws exception
}
catch(Exception $e) {
user_error($e);
}
[/code]
Copy linkTweet thisAlerts:
@azegurbauthorFeb 05.2010 — Thank you i just understood
Copy linkTweet thisAlerts:
@azegurbauthorFeb 05.2010 — Thank you i just understood[/QUOTE] thank you
×

Success!

Help @azegurb 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 5.19,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...