PHP Cookbook读书笔记 – 第07章类和对象
虽说在PHP5之前,php就有对面向 对象 编程方面的支持,但对 对象 和类的处理不是那么的理想,直到PHP5通过使用Zend Engine2(ZE2)才使PHP得以包含高级的面向 对象 特性。本章就是介绍PHP5的面向 对象 特性,目前,很多的公司在招聘PHPer时都会提出对这部分
虽说在PHP5之前,php就有对面向对象编程方面的支持,但对对象和类的处理不是那么的理想,直到PHP5通过使用Zend Engine2(ZE2)才使PHP得以包含高级的面向对象特性。本章就是介绍PHP5的面向对象特性,目前,很多的公司在招聘PHPer时都会提出对这部分的要求。需要稍微注意一下的地方是本书中文版在new一个对象的时候(Instantiating Objects)翻译为技巧化一个对象,而在博主阅读的资料里都将这一行为描述为“实例化”一个对象。
在学习完本章节需要掌握面向对象的3个特性的使用:继承、封装、多态。
封装,就是把客观事物封装成抽象的类,并且类可以把自己的数据和方法只让可信的类或者对象操作,对不可信的进行信息隐藏。
继承,实现方式有三类:实现继承、接口继承和可视继承。
- 实现继承是指使用基类的属性和方法而无需额外编码的能力;
- 接口继承是指仅使用属性和方法的名称、但是子类必须提供实现的能力;
- 可视继承是指子窗体(类)使用基窗体(类)的外观和实现代码的能力。
多态,有二种方式,覆盖,重载。
- 覆盖,是指子类重新定义父类的虚函数的做法。
- 重载,是指允许存在多个同名函数,而这些函数的参数表不同(或许参数个数不同,或许参数类型不同,或许两者都不同)。
类里面的几个特殊方法名
__construct:构造器,执行类时首先运行
__destruct:析构器,类结束前运行
__toString:字符串化,这个方法在PHP5早期版本和PHP5.2之后存在差异,使用需注意
public, protected, private :访问控制,分别代表公共、保护、私有
final :方法不可被继承的子类修改或类不可继承
interface:定义一个接口
abstract :定义一个抽象类
clone :复制一个对象的副本
$adam = new user; //两个变量指向的是一个<strong>对象</strong> $dave = $adam; //指向的是两个不同搜<strong>对象</strong> $john= clone $adam ;
__get($property)和__set($property, $value):属性访问
__isset( ) 和 __unset( ) :判断是否设置或是否销毁,只对PHP5.1之后版本有效
__call($method, $arguments):简单点说就是在调用类中未定义的方法时就调用这个
parent::方法名():在子类中调用父类的方法
const :定义类常量,通过[类名]::[常量名]来调用
static:定义静态方法或属性,有.NET变成经验的同学需要注意,PHP中的静态方法只在本次请求过程中有效而不是像.NET中的静态方法一次调用后会长期驻留内存。
__sleep( ) 和 __wakeUp( ):在序列号和反序列化一个类时调用,常用来保存一些外部资源的连接等
通过反射获得一个类的信息:
Reflection::export(new ReflectionClass('car'));
ReflectionMethod 和 ReflectionFunction:分别是反射一个类方法和反射一个函数
instanceof :检测一个对象是否是指定类的实例
__autoload:在实例化一个为定义的类时,会执行此方法
function __autoload($class_name) { include "$class_name.php"; } $person = new Person;
很蛋疼的PHP语法规则
//合法 $class_name = 'Net_Ping'; $class = new $class_name; // new Net_Ping //下面的非法 $partial_class_name = 'Ping'; $class = new "Net_$partial_class_name"; // new Net_Ping //合法 $partial_class_name = 'Ping'; $class_prefix = 'Net_'; $class_name = "$class_prefix$partial_class_name"; $class = new $class_name; // new Net_Ping

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



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.
