PHP Magic Method Summary 1
PHP In addition to a large number of magic variables, there are many _ at the beginning of the Magic method, this article simple summary under:
1 _construct and _destruct did not say, construction and destruction.
2 _call and _callstatic
Example:
Class MyClass () {
Private $a = true;
}
$MYOBJ = new MyClass ();
$MYOBJ->showvalue ();
?>
There's going to be a mistake here, of course, no definition. can be used
Class Greetings {
function __call ($functionName, $argumentsArray) {
echo "Hello,". Ucfirst ($functionName). " !!!";
}
}
$sayHello = new Greetings ();
_call triggers when a call does not define any method
and a _callstatic.
Class Greetings {
static function __callstatic ($functionName, $argumentsArray) {
echo "Hello,". Ucfirst ($functionName). " !!!";
}
}
Greetings::steve ();
3 _set and _get
This is an example of forcing a variable declared in a class to be used:
Class MyClass {
Private $onlyDataMember = 0;
}
$CLASSOBJ = new MyClass ();
$CLASSOBJ->undefineddatamember = ' Some Value ';
Echo $CLASSOBJ->undefineddatamember;
?>
This is the direct output of the undefined value, not good, can be changed to such, mandatory:
Class MyClass {
Private $onlyDataMember = 0;
function __set ($dataMemberName, $dataMemberValue) {
throw new Exception ("Object property $dataMemberName was not writable!");
}
function __get ($dataMemberName) {
throw new Exception ("Object property $dataMemberName was not defined!");
}
}
echo "Username:". $oUserInfo 2->adata[' UserName '). "
\ n ";
echo "Password:". $oUserInfo 2->adata[' PassWord ']. "
\ n ";
echo "Birthday:" $oUserInfo 2->adata[' Birthday ']. "
\ n ";
Obviously, this code will go wrong, because Adata is a private property of userinfo and cannot be used directly externally, now the problem is that Adata must be paid for, and __set and __get will come in handy at this time:
echo "Username:". $oUserInfo 3->adata[' UserName '). "
\ n ";
echo "Password:". $oUserInfo 3->adata[' PassWord ']. "
\ n ";
echo "Birthday:" $oUserInfo 3->adata[' Birthday ']. "
\ n ";
$CLASSOBJ = new MyClass ();
$CLASSOBJ->undefineddatamember = ' Some Value ';
Echo $CLASSOBJ->undefineddatamember;
?>
This will force an error when the call is made.
4) _invoke
To use objects as functions, typical examples of which we usually use:
Class Mydbusersclass {
Private $recordsArray = Array ();
function __construct ($fetchThisMuchUsers) {
Code for fetching $fetchThisMuchUsers number of records from the database and store them to $recordsArray.
}
function Getusers () {
return $recordsArray;
}
}
$CLASSOBJ = new Mydbusersclass (10);
$users = $CLASSOBJ->getusers ();
foreach ($users as $user) {
echo $user [' UserName ']. '
';
}
?>
You can now use this:
Class Mydbusersclass {
Private $recordsArray = Array ();
function __construct ($fetchThisMuchUsers) {
Code for fetching $fetchThisMuchUsers number of records from the database and store them to $recordsArray.
}
function __invoke ($onlyArgumentItAccepts) {
return $recordsArray;
}
}
$CLASSOBJ = new Mydbusersclass (10);
foreach ($CLASSOBJ () as $user) {
echo $user [' UserName ']. '
';
}
?>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.