PHP魔术方法的使用示例_php实例
① __get/__set:将对象的属性进行接管
当访问一个不存在的对象属性时:
index.php
define('BASEDIR',__DIR__); //定义根目录常量
include BASEDIR.'/Common/Loader.php';
spl_autoload_register('\\Common\\Loader::autoload');
$obj = new \Common\Object();
//在php中访问一个不存在的对象属性时
echo $obj->title;
会抛出一个错误:Notice: Undefined property: Common\Object::$title in D:\practise\php\design\psr0\index.php on line 9
当在Common/Object.php 中添加 __set 和 __get 方法后
Object.php
namespace Common;
class Object{
function __set($key,$value){
}
function __get($key){
}
}
再执行 index.php,不会再报错。
再次修改 Common/Object.php
namespace Common;
class Object{
protected $array = array();
function __set($key,$value){
var_dump(__METHOD__);
$this->array[$key] = $value;
}
function __get($key){
var_dump(__METHOD__);
return $this->array[$key];
}
}
index.php
define('BASEDIR',__DIR__); //定义根目录常量
include BASEDIR.'/Common/Loader.php';
spl_autoload_register('\\Common\\Loader::autoload');
$obj = new \Common\Object();
$obj->title = 'hello';
echo $obj->title;
执行 index.php,页面输出:
string 'Common\Object::__set' (length=20)
string 'Common\Object::__get' (length=20)
hello
② __call/__callStatic:控制 PHP 对象方法的调用(__callStatic 用来控制类的静态方法)
当执行一个不存在的php方法时
index.php:
define('BASEDIR',__DIR__); //定义根目录常量
include BASEDIR.'/Common/Loader.php';
spl_autoload_register('\\Common\\Loader::autoload');
$obj = new \Common\Object();
//当执行一个不存在的php方法时
$obj->test('hello',123);
执行 index.php 会报一个致命错误:Fatal error: Call to undefined method Common\Object::test() in D:\practise\php\design\psr0\index.php on line 9
如果在 Common/Object 中定义一个__call 方法,则会在方法不存在时自动回调:
namespace Common;
class Object{
function __call($func, $param){ //$func 方法名 $param 参数
var_dump($func, $param);
return "magic function\n"; //返回一个字符串作为返回值
}
}
index.php
define('BASEDIR',__DIR__); //定义根目录常量
include BASEDIR.'/Common/Loader.php';
spl_autoload_register('\\Common\\Loader::autoload');
$obj = new \Common\Object();
//当执行一个不存在的php方法时
echo $obj->test('hello',123);
页面输出:
string 'test' (length=4)
array
0 => string 'hello' (length=5)
1 => int 123
magic function
当调用一个不存在的静态方法时
Common/Object.php
namespace Common;
class Object{
static function __callStatic($name, $arguments) {
var_dump($name, $arguments);
return "magic function\n"; //返回一个字符串作为返回值
}
}
注意:__callStatic 方法也要声明成静态方法
index.php
define('BASEDIR',__DIR__); //定义根目录常量
include BASEDIR.'/Common/Loader.php';
spl_autoload_register('\\Common\\Loader::autoload');
//执行一个不存在的静态方法
echo Common\Object::test("hello",1234);
执行 index.php ,页面输出:
string 'test' (length=4)
array
0 => string 'hello' (length=5)
1 => int 1234
magic function
③ __toString:将一个 PHP 对象转换成一个字符串
index.php
define('BASEDIR',__DIR__); //定义根目录常量
include BASEDIR.'/Common/Loader.php';
spl_autoload_register('\\Common\\Loader::autoload');
$obj = new \Common\Object();
echo $obj;
此时会报错: Catchable fatal error: Object of class Common\Object could not be converted to string in D:\practise\php\design\psr0\index.php on line 8
在 Object.php 中添加 __toString 方法
namespace Common;
class Object{
function __toString() {
return __CLASS__;
}
}
④ __invoke:将一个 PHP 对象当成一个函数来执行时,会回调此魔术方法
index.php
define('BASEDIR',__DIR__); //定义根目录常量
include BASEDIR.'/Common/Loader.php';
spl_autoload_register('\\Common\\Loader::autoload');
$obj = new \Common\Object();
echo $obj("test");
Object.php
namespace Common;
class Object{
function __invoke($param) {
var_dump($param);
return 'invoke';
}
}
页面输出:
string 'test' (length=4)
invoke

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック









PHP 8.4 では、いくつかの新機能、セキュリティの改善、パフォーマンスの改善が行われ、かなりの量の機能の非推奨と削除が行われています。 このガイドでは、Ubuntu、Debian、またはその派生版に PHP 8.4 をインストールする方法、または PHP 8.4 にアップグレードする方法について説明します。

CakePHP でデータベースを操作するのは非常に簡単です。この章では、CRUD (作成、読み取り、更新、削除) 操作について理解します。

ファイルのアップロードを行うには、フォーム ヘルパーを使用します。ここではファイルアップロードの例を示します。

CakePHP は、PHP 用のオープンソース フレームワークです。これは、アプリケーションの開発、展開、保守をより簡単にすることを目的としています。 CakePHP は、強力かつ理解しやすい MVC のようなアーキテクチャに基づいています。モデル、ビュー、コントローラー

CakePHP へのログインは非常に簡単な作業です。使用する関数は 1 つだけです。 cronjob などのバックグラウンド プロセスのエラー、例外、ユーザー アクティビティ、ユーザーが実行したアクションをログに記録できます。 CakePHP でのデータのログ記録は簡単です。 log()関数が提供されています

Visual Studio Code (VS Code とも呼ばれる) は、すべての主要なオペレーティング システムで利用できる無料のソース コード エディター (統合開発環境 (IDE)) です。 多くのプログラミング言語の拡張機能の大規模なコレクションを備えた VS Code は、
