PHP Magic 1

Source: Internet
Author: User
PHP magic methods summary 1PHP in addition to a large number of magic variables, there are also a lot of magic methods at the beginning of the _, a simple summary of this article: construct and _ destruct will not talk about, structure and analysis structure. 2_call and _ callStatic & nbsp; example: & nbsp; & lt ;? Php & nb PHP magic methods summary 1
In addition to a large number of magic variables, PHP also has a lot of magic methods starting with _. This article briefly summarizes:

1 _ construct and _ destruct won't be mentioned, structure and analysis structure.

2 _ call and _ callStatic
Example:
Class myClass (){
Private $ a = true;
}

$ MyObj = new myClass ();
$ MyObj-> showValue ();

?>
Errors will be mentioned here. of course they are not defined. they can be used.
Class Greetings {
Function _ call ($ functionName, $ argumentsArray ){
Echo "Hello,". ucfirst ($ functionName )."!!! ";
}
}
$ SayHello = new Greetings ();
_ CALL is triggered when no method is defined in the CALL.

And _ callStatic
Class Greetings {
Static function _ callStatic ($ functionName, $ argumentsArray ){
Echo "Hello,". ucfirst ($ functionName )."!!! ";
}
}

Greetings: steve ();


3 _ set and _ get
This is to force variables declared in the class to be used. for example:
Class myClass {
Private $ onlyDataMember = 0;
}
$ ClassObj = new myClass ();
$ ClassObj-> undefinedDataMember = 'some value ';
Echo $ classObj-> undefinedDataMember;
?>
This is an undefined value directly output, which is not very good. you can change it to this, which is mandatory:
Class myClass {
Private $ onlyDataMember = 0;

Function _ set ($ dataMemberName, $ dataMemberValue ){
Throw new Exception ("Object property $ dataMemberName is not writable! ");
}

Function _ get ($ dataMemberName ){
Throw new Exception ("Object property $ dataMemberName is not defined! ");
}
}

Another example:
Class UserInfo2 {

Private $ aData = array ();

}

$ OUserInfo2 = new UserInfo2;

$ OUserInfo2-> aData ['username'] = 'Mu Muzi ';

$ OUserInfo2-> aData ['password'] = '000000 ';

$ OUserInfo2-> aData ['birthdat '] = '2017-08-16 ';

Echo "UserName:". $ oUserInfo2-> aData ['username']."
\ N ";

Echo "PassWord:". $ oUserInfo2-> aData ['password']."
\ N ";

Echo "Birthdate:". $ oUserInfo2-> aData ['birthday']."
\ N ";

Obviously, this code will cause an error. because aData is a private property of UserInfo and cannot be used externally, the problem is that aData must be paid, in this case, _ set and _ get come in handy:

Class UserInfo3 {

// Private $ aData = array ();

Private $ aData = array ();

Function _ set ($ property, $ value ){

$ This-> aData [$ property] = $ value;

}

Function _ get ($ property ){

Return $ this-> aData [$ property];

}

}

$ OUserInfo3 = new UserInfo3;

$ OUserInfo3-> aData ['username'] = 'Mu Muzi ';

$ OUserInfo3-> aData ['password'] = '000000 ';

$ OUserInfo3-> aData ['birthdat '] = '2017-08-16 ';

Echo "UserName:". $ oUserInfo3-> aData ['username']."
\ N ";

Echo "PassWord:". $ oUserInfo3-> aData ['password']."
\ N ";

Echo "Birthdate:". $ oUserInfo3-> aData ['birthday']."
\ N ";




$ ClassObj = new myClass ();
$ ClassObj-> undefinedDataMember = 'some value ';
Echo $ classObj-> undefinedDataMember;

?>
In this way, an error is forcibly reported during the call.

4) _ invoke
An object is used as a function. a typical example is:
Class myDbUsersClass {
Private $ recordsArray = array ();

Function _ construct ($ fetchThisMuchUsers ){
// Code for fetching $ fetchThisMuchUsers number of records from 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 use the following code:
Class myDbUsersClass {
Private $ recordsArray = array ();

Function _ construct ($ fetchThisMuchUsers ){
// Code for fetching $ fetchThisMuchUsers number of records from database and store them to $ recordsArray.
}

Function _ invoke ($ onlyArgumentItAccepts ){
Return $ recordsArray;
}
}

$ ClassObj = new myDbUsersClass (10 );
Foreach ($ classObj () as $ user ){
Echo $ user ['username'].'
';
}
?>
 

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.