background image

PHP 面向对象学习

基础概念

1> 

if

( "false" ) 

 

等效于

if

( true), 因为非空字符串是 true 

2> 检查数据类型: 

is_array

(); 

is_object

(); 

is_string

(); 

is_null

(); 

is_integer

(); 

3> PHP5 引入类的类型提示(type hint),用来约束一个方法的参数类型(不是基本数据类型,
而是类):将类名放在需要约束的方法参数之前. 
例如: 

function

 write( ShopProduct 

$shopProduct

){} 

 
4> 

instanceof

 操作符: 如果左边操作数的对象是右边操作数所示的类型,结果为 true 

例如: 

if

$shopProduct

 

instanceof

 BookProduct ) {} 

 
5> 

 

继承

class

 son 

extends

 parent{} 

要调用父类的方法, 比如构造函数,  

用 parent::__construct(); 

 
6> 

 

静态方法和属性

class

 StaticExample{ 

static

 

public

 

$a

static

 

public

 

function

 hello(){} 


外部访问使用:: 
例如: 

print

 StaticExample::

$a

内部访问使用 self:: 
例如: self::

$a

 
7> 抽象类, 

 

抽象方法

abstract

 

class

 xxx{ 

... 

abstract

 

function

 write(); 

//没有{} 


 
抽象类的子类要重新声明方法并实现之. 新实现的方法的访问控制不能比抽象方法的访问
控制更严格. 
 
8>

 

接口

interface

 

只定义功能,不包含实现. 接口中可以包含属性和方法声明,但方法体为空; 
例如: 

interface

 a{