PHP幻术方法小结1
程序员文章站
2022-05-12 18:08:56
...
PHP魔术方法小结1
PHP中除了有大量的魔术变量外,还有很多_开头的魔术方法,本文简单小结下:
1 _construct 和_destruct就不说了,构造和析构.
2 _call和_callStatic
例子:
class myClass() {
private $a = true;
}
$myObj = new myClass();
$myObj->showValue();
?>
这里会说出错,那当然的,没定义.可以用
class Greetings {
function __call( $functionName, $argumentsArray ) {
echo "Hello, " . ucfirst( $functionName ) . " !!!";
}
}
$sayHello = new Greetings();
_CALL会在调用没有定义任何方法时触发
还有_callStatic
class Greetings {
static function __callStatic( $functionName, $argumentsArray ) {
echo "Hello, " . ucfirst( $functionName ) . " !!!";
}
}
Greetings::steve();
3 _set和_get
这个是强制在类中声明的变量才能被使用,一个例子:
class myClass {
private $onlyDataMember = 0;
}
$classObj = new myClass();
$classObj->undefinedDataMember = 'Some Value';
echo $classObj->undefinedDataMember;
?>
这个是直接输出了没定义的值,不大好,可以改成这样,强制性地:
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!" );
}
}
再来一个例子:
class UserInfo2{
private $aData = array();
}
$oUserInfo2 = new UserInfo2;
$oUserInfo2->aData['UserName'] = '木目子';
$oUserInfo2->aData['PassWord'] = '123456';
$oUserInfo2->aData['Birthdat'] = '1978-08-16';
echo "用户名:".$oUserInfo2->aData['UserName'] ."
\n";
echo "密 码:".$oUserInfo2->aData['PassWord'] ."
\n";
echo "生 日:".$oUserInfo2->aData['Birthday'] ."
\n";
显然,这段代码会出错的,因为aData是UserInfo的私有属性,不能直接在外部使用,那现在问题是必须要给aData进行付值,这个时候__set和__get就排上用场了:
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'] = '木目子';
$oUserInfo3->aData['PassWord'] = '123456';
$oUserInfo3->aData['Birthdat'] = '1978-08-16';
echo "用户名:".$oUserInfo3->aData['UserName'] ."
\n";
echo "密 码:".$oUserInfo3->aData['PassWord'] ."
\n";
echo "生 日:".$oUserInfo3->aData['Birthday'] ."
\n";
$classObj = new myClass();
$classObj->undefinedDataMember = 'Some Value';
echo $classObj->undefinedDataMember;
?>
这样当调用的时候,就强制报错了
4) _invoke
把对象当函数用,典型的例子为,平常我们一般这样用:
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' ] . '
';
}
?>
现在可以这样用:
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' ] . '
';
}
?>
PHP中除了有大量的魔术变量外,还有很多_开头的魔术方法,本文简单小结下:
1 _construct 和_destruct就不说了,构造和析构.
2 _call和_callStatic
例子:
class myClass() {
private $a = true;
}
$myObj = new myClass();
$myObj->showValue();
?>
这里会说出错,那当然的,没定义.可以用
class Greetings {
function __call( $functionName, $argumentsArray ) {
echo "Hello, " . ucfirst( $functionName ) . " !!!";
}
}
$sayHello = new Greetings();
_CALL会在调用没有定义任何方法时触发
还有_callStatic
class Greetings {
static function __callStatic( $functionName, $argumentsArray ) {
echo "Hello, " . ucfirst( $functionName ) . " !!!";
}
}
Greetings::steve();
3 _set和_get
这个是强制在类中声明的变量才能被使用,一个例子:
class myClass {
private $onlyDataMember = 0;
}
$classObj = new myClass();
$classObj->undefinedDataMember = 'Some Value';
echo $classObj->undefinedDataMember;
?>
这个是直接输出了没定义的值,不大好,可以改成这样,强制性地:
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!" );
}
}
再来一个例子:
class UserInfo2{
private $aData = array();
}
$oUserInfo2 = new UserInfo2;
$oUserInfo2->aData['UserName'] = '木目子';
$oUserInfo2->aData['PassWord'] = '123456';
$oUserInfo2->aData['Birthdat'] = '1978-08-16';
echo "用户名:".$oUserInfo2->aData['UserName'] ."
\n";
echo "密 码:".$oUserInfo2->aData['PassWord'] ."
\n";
echo "生 日:".$oUserInfo2->aData['Birthday'] ."
\n";
显然,这段代码会出错的,因为aData是UserInfo的私有属性,不能直接在外部使用,那现在问题是必须要给aData进行付值,这个时候__set和__get就排上用场了:
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'] = '木目子';
$oUserInfo3->aData['PassWord'] = '123456';
$oUserInfo3->aData['Birthdat'] = '1978-08-16';
echo "用户名:".$oUserInfo3->aData['UserName'] ."
\n";
echo "密 码:".$oUserInfo3->aData['PassWord'] ."
\n";
echo "生 日:".$oUserInfo3->aData['Birthday'] ."
\n";
$classObj = new myClass();
$classObj->undefinedDataMember = 'Some Value';
echo $classObj->undefinedDataMember;
?>
这样当调用的时候,就强制报错了
4) _invoke
把对象当函数用,典型的例子为,平常我们一般这样用:
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' ] . '
';
}
?>
现在可以这样用:
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' ] . '
';
}
?>
相关文章
相关视频