首页 php教程 php手册 PHP面向对象中的重要知识点(二)

PHP面向对象中的重要知识点(二)

Jun 13, 2016 am 09:46 AM
php 面向对象

1. __toString:

 

    当对象被打印时,如果该类定义了该方法,则打印该方法的返回值,否则将按照PHP的缺省行为输出打印结果。该方法类似于Java中的toString()。

 

复制代码

class TestClass {

    public function __toString() {

        return "This is TestClass::__toString.\n";

    }

}

 

$testObj = new TestClass();

print $testObj;

复制代码

    运行结果如下:

 

Stephens-Air:Desktop$ php Test.php 

This is TestClass::__toString.

2. __get和__set:

 

    这两个方法用于处理类中未声明的属性访问。当对象使用者试图访问未声明的对象属性时,__get()会被调用,并带有一个包含要访问的属性名称字符串作为参数。无论从__get()方法返回什么,都会直接返回给调用者,就如同带有该值的属性存在一样。另外需要注意的是,如果属性存在,但是其访问可见性为private或protected,那么这两个拦截方法同样会被调用,反之,如果属性存在切可访问,那么直接访问属性即可,这两个方法将不再会被调用。以下为__get()拦截方法的示例代码:

 

复制代码

class TestClass {

    private $privateField;

    public $publicField;

    public function __construct() {

        $this->privateField = "This is a private Field.\n";

        $this->publicField = "This is a public Field.\n";

    }

 

    public function __get($property) {

        print "__get() is called.\n";

        $method = "get${property}";

        if (method_exists($this, $method)) {

            return $this->$method();

        }

        return "This is undefined field.\n";

    }

    public function getPrivateField() {

        return $this->privateField;

    }

}

 

$testObj = new TestClass();

print $testObj->privateField;

print $testObj->undefinedField;

print $testObj->publicField;

复制代码

    运行结果如下:

 

Stephens-Air:Desktop$ php Test.php 

__get() is called.

This is a private Field.

__get() is called.

This is undefined field.

This is a public Field.

    __set()方法被调用的规则和__get()基本相同,差别是用于拦截未定义或不可见类属性的赋值操作。另外,该方法接收两个参数,分别是属性名称和要设定的值。见如下代码示例:

 

复制代码

class TestClass {

    private $privateField;

    public $publicField;

    public function __construct() {

        $this->privateField = "This is a private Field.\n";

        $this->publicField = "This is a public Field.\n";

    }

    public function __get($property) {

        print "__get() is called.\n";

        $method = "get${property}";

        if (method_exists($this, $method)) {

            return $this->$method();

        }

        return "This is an undefined field.\n";

    }

    public function __set($property, $value) {

        print "__set is called.\n";

        $method = "set${property}";

        if (method_exists($this, $method)) {

            $this->$method($value);

        } else {

            print "This is an undefined field.\n";

        }

    }

    public function getPrivateField() {

        return $this->privateField;

    }

    public function setPrivateField($value) {

        $this->privateField = $value;

    }

}

 

$testObj = new TestClass();

$testObj->privateField = "This is a private Field after set.\n";

$testObj->undefinedField = "This is a undefined Field after set.\n";

$testObj->publicField = "This is a public Field after set.\n";

 

print $testObj->privateField;

print $testObj->undefinedField;

print $testObj->publicField;

复制代码

    运行结果如下:

 

复制代码

Stephens-Air:Desktop$ php Test.php 

__set is called.

__set is called.

This is an undefined field.

__get() is called.

This is a private Field after set.

__get() is called.

This is an undefined field.

This is a public Field after set.

复制代码

3. __isset和__unset:

 

    这两个拦截方法被调用的规则和__get()和__set()非常类似,只是用于类中不存在或不可见属性被isset()和unset()两个全局方法应用时才会被分别触发。 

 

复制代码

class TestClass {

    private $privateField;

    public $publicField;

    public function __construct() {

        $this->privateField = "Defined private field";

        $this->publicField = "Defined public field";

    }

    public function __isset($property) {

        print "__isset is called.\n";

        return isset($this->$property);

    }

    public function __unset($property) {

        print "__unset is called.\n";

        if (isset($this->$property)) {

            unset($this->$property);

        }

    }

}

 

$testObj = new TestClass();

print 'isset($testObj->privateField) is '.(isset($testObj->privateField) ? "true" : "false")."\n";

print 'isset($testObj->undefinedField) is '.(isset($testObj->undefinedField) ? "true" : "false")."\n";

print 'isset($testObj->publicField) is '.(isset($testObj->publicField) ? "true" : "false")."\n";

 

print "After unset......\n";

//下面两个函数调用后,$testObj的两个对象属性均会变为不可用。

//另外从输出结果来看,__unset方法仅仅被调用一次,因为publicField为可见属性,所以__unset不会因该属性而被调用。

unset($testObj->privateField);

unset($testObj->publicField);

 

print 'isset($testObj->privateField) is '.(isset($testObj->privateField) ? "true" : "false")."\n";

print 'isset($testObj->publicField) is '.(isset($testObj->publicField) ? "true" : "false")."\n";

复制代码

    运行结果如下:

 

复制代码

Stephens-Air:Desktop$ php Test.php 

__isset is called.

isset($testObj->privateField) is true

__isset is called.

isset($testObj->undefinedField) is false

isset($testObj->publicField) is true

After unset......

__unset is called.

__isset is called.

isset($testObj->privateField) is false

__isset is called.

isset($testObj->publicField) is false

复制代码

4. __call:

 

    __call()方法是一个非常有用但又非常容易被滥用的拦截方法。当对象使用者试图访问当前对象未定义的成员函数时,__call()会被自动调用,同时传递两个参数,分别为函数名称和传递给调用函数的所有参数(数组)。__call方法返回的任何值都会返回给函数调用者,就如同该成员函数真实存在一样。下面给出一个非常有用的委托示例。 

 

复制代码

class DelegateClass {

    function printMessage($arg1, $arg2) {

        print "DelegateClass:delegatedMethod is called.\n";

        print '$arg1 = '.$arg1.'and $arg2 = '.$arg2."\n";

    }

}

class TestClass {

    private $delegateObj;

    public function __construct() {

        $this->delegateObj = new DelegateClass();

    }

    public function __call($method, $args) {

        $this->delegateObj->$method($args[0],$args[1]);

    }

}

 

$testObj = new TestClass();

$testObj->printMessage("hello","world");

复制代码

    运行结果如下:

 

Stephens-Air:Desktop$ php Test.php 

DelegateClass:delegatedMethod is called.

$arg1 = helloand $arg2 = world

    从以上示例可以看出,TestClass并未声明printMessage成员方法,但是通过__call()方法的巧妙桥接直接传递给了委托对象。个人认为该技巧为双刃剑,切勿过度使用。

 

5. 回调函数: 

 

    回调函数的应用场景无须多述,在C/C++中充斥着无数的回调函数典型用例。 这里只是简单给出PHP中回调函数的使用规则。见如下示例代码和关键性注释: 

 

复制代码

class Product {

    public $name;

    public $price;

    public function __construct($name, $price) {

        $this->name = $name;

        $this->price = $price;

    }

}

 

class ProcessSale {

    private $callbacks;

    function registerCallback($cb) {

        if (!is_callable($cb)) {

            throw new Exception("callback not callable.");

        }

        $this->callbacks[] = $cb;

    }

    function sale($product) {

        print "{$product->name}: processing \n";

        foreach ($this->callbacks as $cb) {

            //以下两种调用方式均可。

            call_user_func($cb, $product);

            $cb($product);

        }

    }

}

 

$logger = function($product) {

    print "    logging ({$product->name})\n";

};

 

$processor = new ProcessSale();

$processor->registerCallback($logger);

$processor->sale(new Product("shoes",6));

print "\n";

$processor->sale(new Product("coffee",6));

复制代码

    运行结果如下:

 

复制代码

Stephens-Air:Desktop$ php Test.php 

shoes: processing 

    logging (shoes)

    logging (shoes)

 

coffee: processing 

    logging (coffee)

    logging (coffee)

复制代码

6. use(闭包):

 

    在Javascript中存在大量的闭包应用,PHP中的闭包则是通过use关键字来完成的。对于闭包这个概念本身而言,简要的说就是函数内的代码可以访问其父作用域中的变量。见如下示例代码和关键性注释:

 

复制代码

class Product {

    public $name;

    public $price;

    public function __construct($name, $price) {

        $this->name = $name;

        $this->price = $price;

    }

}

 

class ProcessSale {

    private $callbacks;

    function registerCallback($cb) {

        if (!is_callable($cb)) {

            throw new Exception("callback not callable.");

        }

        $this->callbacks[] = $cb;

    }

    function sale($product) {

        print "{$product->name}: processing \n";

        foreach ($this->callbacks as $cb) {

            $cb($product);

        }

    }

}

 

class Totalizer {

    static function warnAmount($amt) {

        $count = 0;

        //注意这里的$amt和$count均为闭包变量,其中&$count是以引用的形式传递的,即一旦函数内部修改了该变量的值,

        //那么下次再访问该闭包变量时,$count将为之前调用中修改后的值。

        return function($product) use($amt, &$count) {

            $count += $product->price;

            print "     count: $count\n";

            if ($count > $amt) {

                print "     high price reached: {$count}\n";

            }

        };

    }

}

 

$processor = new ProcessSale();

$processor->registerCallback(Totalizer::warnAmount(8));

$processor->sale(new Product("shoes",6));

$processor->sale(new Product("coffee",6));

复制代码

    运行结果如下:

 

shoes: processing 

     count: 6

coffee: processing 

     count: 12

     high price reached: 12

注:该Blog中记录的知识点,是在我学习PHP的过程中,遇到的一些PHP和其他面向对象语言相比比较独特的地方,或者是对我本人而言确实需要簿记下来以备后查的知识点。虽然谈不上什么深度,但是还是希望能与大家分享。

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

<🎜>:泡泡胶模拟器无穷大 - 如何获取和使用皇家钥匙
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系统,解释
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆树的耳语 - 如何解锁抓钩
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

Java教程
1664
14
CakePHP 教程
1423
52
Laravel 教程
1321
25
PHP教程
1269
29
C# 教程
1249
24
PHP和Python:比较两种流行的编程语言 PHP和Python:比较两种流行的编程语言 Apr 14, 2025 am 12:13 AM

PHP和Python各有优势,选择依据项目需求。1.PHP适合web开发,尤其快速开发和维护网站。2.Python适用于数据科学、机器学习和人工智能,语法简洁,适合初学者。

PHP行动:现实世界中的示例和应用程序 PHP行动:现实世界中的示例和应用程序 Apr 14, 2025 am 12:19 AM

PHP在电子商务、内容管理系统和API开发中广泛应用。1)电子商务:用于购物车功能和支付处理。2)内容管理系统:用于动态内容生成和用户管理。3)API开发:用于RESTfulAPI开发和API安全性。通过性能优化和最佳实践,PHP应用的效率和可维护性得以提升。

PHP:网络开发的关键语言 PHP:网络开发的关键语言 Apr 13, 2025 am 12:08 AM

PHP是一种广泛应用于服务器端的脚本语言,特别适合web开发。1.PHP可以嵌入HTML,处理HTTP请求和响应,支持多种数据库。2.PHP用于生成动态网页内容,处理表单数据,访问数据库等,具有强大的社区支持和开源资源。3.PHP是解释型语言,执行过程包括词法分析、语法分析、编译和执行。4.PHP可以与MySQL结合用于用户注册系统等高级应用。5.调试PHP时,可使用error_reporting()和var_dump()等函数。6.优化PHP代码可通过缓存机制、优化数据库查询和使用内置函数。7

PHP的持久相关性:它还活着吗? PHP的持久相关性:它还活着吗? Apr 14, 2025 am 12:12 AM

PHP仍然具有活力,其在现代编程领域中依然占据重要地位。1)PHP的简单易学和强大社区支持使其在Web开发中广泛应用;2)其灵活性和稳定性使其在处理Web表单、数据库操作和文件处理等方面表现出色;3)PHP不断进化和优化,适用于初学者和经验丰富的开发者。

PHP与Python:了解差异 PHP与Python:了解差异 Apr 11, 2025 am 12:15 AM

PHP和Python各有优势,选择应基于项目需求。1.PHP适合web开发,语法简单,执行效率高。2.Python适用于数据科学和机器学习,语法简洁,库丰富。

PHP和Python:代码示例和比较 PHP和Python:代码示例和比较 Apr 15, 2025 am 12:07 AM

PHP和Python各有优劣,选择取决于项目需求和个人偏好。1.PHP适合快速开发和维护大型Web应用。2.Python在数据科学和机器学习领域占据主导地位。

PHP与其他语言:比较 PHP与其他语言:比较 Apr 13, 2025 am 12:19 AM

PHP适合web开发,特别是在快速开发和处理动态内容方面表现出色,但不擅长数据科学和企业级应用。与Python相比,PHP在web开发中更具优势,但在数据科学领域不如Python;与Java相比,PHP在企业级应用中表现较差,但在web开发中更灵活;与JavaScript相比,PHP在后端开发中更简洁,但在前端开发中不如JavaScript。

PHP和Python:解释了不同的范例 PHP和Python:解释了不同的范例 Apr 18, 2025 am 12:26 AM

PHP主要是过程式编程,但也支持面向对象编程(OOP);Python支持多种范式,包括OOP、函数式和过程式编程。PHP适合web开发,Python适用于多种应用,如数据分析和机器学习。

See all articles