给猪的鼻子插一根葱_PHP
PHP 具备一些动态语言的特征, 但不彻底. 虽然 PHP 的标志是一头大象, 可这头象的鼻子未免太短, 以致经常够不着东西, 反而象猪了. 本文旨在探讨一种使 PHP 更动态化的方法, 主要是模拟 Javascript 的 prototype 继承. 既然是模拟, 就不是真的能使 PHP 动态起来, 只是插上一根葱, 让它装得更"象"一点.
一. 基本操作
通过 Javascript 的 prototype 动态地为对象添加属性, 我们可以这样:
Object.prototype.greeting = 'Hello' var o = new Object alert(o.greeting)
Js 的内置对象 Object 可看作一个"类", 任何 Js "类"都有 prototype 内置对象, 用 PHP 来模拟它可以是:
error_reporting(E_ALL); class Object { public static $prototype; protected function __get($var) { if ( isset(self::$prototype->$var) ) { return self::$prototype->$var; }} }
然后我们可以:
Object::$prototype->greeting = 'Hello'; $o = new Object; echo $o->greeting; // 输出 Hello
这里利用了 PHP 的自动转型特性. 在 PHP 中, 我们要声明一个数组, 并不需要先 $var = array() 然后才做 $var[] = some_value, 直接地使用后者就可以得到一个数组; 同样地直接 $object->var 的时候, $object 就被自动定义为 stdClass 对象. 这就解决了在定义类内静态属性时不能声明 public static $prototype = new stdClass 的问题.
在 Js 中给"类"动态添加方法:
Object.prototype.say = function(word) { alert(word) } o.say('Hi')
在 PHP 中模拟:
error_reporting(E_ALL); class Object { public static $prototype; protected function __get($var) { if ( isset(self::$prototype->$var) ) { return self::$prototype->$var; }} protected function __call($call, $params) { if ( isset(self::$prototype->$call) && is_callable(self::$prototype->$call) ) { return call_user_func_array(self::$prototype->$call, $params); } else { throw new Exception('Call to undefined method: ' . __CLASS__ . "::$call()"); }} }
这样, 就可以
Object::$prototype->say = create_function('$word', 'echo $word;'); $o->say('Hi');
但是 PHP 的 create_function 返回的结果并不等同于 Js 中的 Function 对象, Js 的 Function 对象是一种闭包(closure), 它可以直接调用宿主的属性, 如
Object.prototype.rock = function() { alert(this.oops) } o.oops = 'Oops' o.rock()
但是在 PHP 中我们不可以写
Object::$prototype->rock = create_function('', 'echo $this->oops;'); $o->oops = 'Oops'; $o->rock();
会报告 Fatal error: Using $this when not in object context, 因为 create_function 返回的是匿名的普通函数, 它没有宿主. 为解决这个问题, 我们需要在参数中传入对象本身, 而且不能使用 $this 变量名做参数, 我们暂时用一个 $caller 的变量名:
Object::$prototype->rock = create_function('$caller', 'echo $caller->oops;'); $o->oops = 'Oops'; $o->rock($o);
现在可以了, 可是看上去怪怪的, 一点都不像动态语言. 嗯~, 这根葱还是有点短, 还是不"象".
问题来了:
1. 在调用动态方法时需要传递对象本身, 这算哪门子的面向对象?
2. 我们要在代码中使用 $this, 这才象是在面向对象.
解决方法:
1. 重新写一个函数代替 create_function, 在参数部分挤一个参数 $that 进去作为第一个参数, 在 __call 中向匿名函数传递参数时加入对象本身 $this 作为第一参数.
2. 允许在代码中使用 $this, 我们在代替函数中把 $this 换成 $that.
我们给它添加一个 create_method 函数来代替 create_function
function create_method($args, $code) { if ( preg_match('/\$that\b/', $args) ) { throw new Exception('Using reserved word \'$that\' as argument'); } $args = preg_match('/^\s*$/s', $args) ? '$that' : '$that, '. $args; $code = preg_replace('/\$this\b/', '$that', $code); return create_function($args, $code); }
$that 作为参数中的"保留字", 当出现在参数部分中将抛出异常.(在 PHP5 的早期暗夜版本中, $that 也曾经是保留字)
相应地, Object 中的 __call 也要作出改动
class Object { public static $prototype; protected function __get($var) { if ( isset(self::$prototype->$var) ) { return self::$prototype->$var; }} protected function __call($call, $params) { if ( isset(self::$prototype->$call) && is_callable(self::$prototype->$call) ) { array_unshift($params, $this); // 这里! return call_user_func_array(self::$prototype->$call, $params); } else { throw new Exception('Call to undefined method: ' . __CLASS__ . "::$call()"); }} }
现在我们就可以
Object::$prototype->rock = create_method('', 'echo $this->oops;'); $o->oops = 'Oops'; $o->rock();
二. 继承
面向对象的一大特征是继承, 继承最大限度地保留代码重用能力. 但如果直接用上例的 Object 类去创建继承类则会出错, 因为
1. 子类继承的静态属性 $prototype 永远属于父类(不管 $prototype 是标量还是列表, 对象更不消说)
2. 如果子类所继承的方法中有 self 关键字, self 会指向父类而非子类
class Object { public static $prototype; protected function __get($var) { ... } protected function __call($call, $params) { ... } } class Test extends Object { } Test::$prototype->greeting = 'Hello'; print_r(Object::$prototype); /* outputs stdClass Object ( [greeting] => Hello ) */ Test::$prototype->say = create_method('$word', 'echo $word;'); $o = new Object; $o->say('Hi'); /* outputs Hi */

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Many users are not sure which one is worth buying, Xiaomi Mi Pad 6 or Mi Pad 6. From the configuration point of view, the higher configuration of Pro is definitely recommended, but it also depends on the price and their own budget, and the different product positioning groups are also different. Which one is worth buying, Xiaomi Mi Pad 6 or Pro? Answer: Xiaomi Mi Pad 6 Pro will be better, both in terms of performance and camera battery life. 1. Processor Xiaomi Pad 6: Snapdragon 870 Xiaomi Mi Pad 6 Pro: more powerful first-generation Snapdragon 8+ processor 2. Battery size Xiaomi Mi Pad 6: 8840mAh, 67W second charge Xiaomi Mi Pad 6 Pro: 8600mAh, 33W The fast-charging Xiaomi Mi Pad 6 will have better battery life, but charging will be slower. 3. Take photos with Xiaomi Mi Pad 6

iPhone 15 Pro vs. iPhone 14 Pro: Specs Comparison Here is a spec comparison between iPhone 15 Pro Max and iPhone 14 Pro Max: iPhone 15 Pro Max iPhone 14 Pro Max Display size 6.7 inches 6.7 inches Display technology Super Retina 2,000 nits Dimensions 6.29x3.02x0.32 inches 6.33x3.06x0.31 inches Weight 221 grams 240 grams

Many users don’t know how to turn on the Xiaomi Mi Band 8 when they first come into contact with it. In fact, the method is very simple. We only need to find the USB data cable and connect the watch to the power supply, and then charge it for a while. You can press the button to turn on. How to turn on Xiaomi Mi Band 8pro 1. First install the watch, that is, connect the theme and the strap on both sides. 2. Then when we turn on the watch for the first time, we need to connect the charging cable. The connection method is at the bottom of the watch. 3. Wait for the watch to vibrate to power on, and then connect to your phone. >>>

MacBook Air is an Apple laptop. Many users are curious about the differences between MacBook Air and Pro. These two notebooks have some differences in processor core, main frequency and graphics card type. The differences between MacBook Air and Pro: 1. Different processor cores and threads: MacBook Air's processor has dual cores and four threads. The Pro is more powerful than the MacBook Air, with four cores and eight threads. 2. The main frequency of the processor is different: the processor of MacBook Air has a main frequency of 1.6GHz. The main frequency of the pro's processor is 1.4GHz, which is slightly lower than that of the macbook air. 3. Different graphics cards: macbookair adopts

JSON (JavaScriptObjectNotation) is a lightweight data exchange format that has become a common format for data exchange between web applications. PHP's json_encode() function can convert an array or object into a JSON string. This article will introduce how to use PHP's json_encode() function, including syntax, parameters, return values, and specific examples. Syntax The syntax of the json_encode() function is as follows: st

Apple continues to push the boundaries of technology, and the latest proof of its innovation is the launch of the Apple A17 Pro GPU. This cutting-edge component features a completely redesigned GPU that promises significant improvements in performance, graphics, and user experience. The Apple A17 Pro GPU, launched alongside the iPhone 15 Pro, is a marvel of semiconductor engineering. It's manufactured using a 3nm process, which represents a major leap in miniaturization, packing more transistors into a single chip than ever before. This technical feat translates into greater power efficiency and computing power. Apple A17 Pro GPU delivers stunning graphics One of the standout features of the A17 Pro chip is its redesigned GPU. Apple has invested heavily in optimizing graphics performance, making it

The Request object in PHP is an object used to handle HTTP requests sent by the client to the server. Through the Request object, we can obtain the client's request information, such as request method, request header information, request parameters, etc., so as to process and respond to the request. In PHP, you can use global variables such as $_REQUEST, $_GET, $_POST, etc. to obtain requested information, but these variables are not objects, but arrays. In order to process request information more flexibly and conveniently, you can

Have you installed cameras in your home? In recent years, home camera products have suddenly become a hit. I asked my friends around me, and boy, every house has one, and some even have more than one. However, with the popularity of the product, it has also brought some complaints during use. For example, you can only see one place at the same time, and if you want to see other places, you have to adjust the pan/tilt and rotate the camera back and forth. There is a certain blind spot and time difference when viewing the picture; or when you want to take a closer look at a certain location in your home, you find that the picture is blurry and you cannot see it at all. Clarity; etc... The experience is greatly compromised. Honor Select and Xiaopai Technology jointly launched the Honor Select Xiaopai Smart Camera Pro featuring "three lenses and dual images". Provides new solutions to industry and user pain points
