php源码 面向对象,PHP面向对象代码解析

这篇博客详细介绍了PHP的面向对象编程概念,包括类的静态成员、常量、私有字段、构造函数、析构函数以及访问控制。此外,还讲解了抽象类、接口、继承和多态性等核心概念。示例代码展示了如何创建和使用这些特性,并通过`runObject`函数展示了多态性。最后,列举了一些PHP内置的对象函数,如`__autoload`、`call_user_method_array`等。
摘要由CSDN通过智能技术生成

您的位置:首页 - 教程 - PHP - 正文

PHP面向对象代码解析

class Point

{

//静态成员,类内被使用self::$size,在类外Point::size

static $size=10;

//常量,使用时可以不用$

const PI=301415926;

//私有字段

private $id;

private $x;

private $y;

//使用自定义的方法进行访问控制

function getId()

{

return $this->id;

}

//提供对私有字段的访问控制

function __get($property_name)

{

if(isset($this->$property_name))

{

return $this->$property_name;

}

else

{

return NULL;

}

}

//提供对私有字段的写入控制

function __set($property_name,$value)

{

if(is_double($value))

{

$this->$property_name=value;

}

else

{

echo "error: value is not a double number.";

}

}

//构造函数

function __construct($x,$y,$id=0)

{

$this->x=$x;

$this->y=$y;

$this->id=$id;

}

function Print_info()

{

echo $this->id,"
";

echo $this->x,"
";

echo $this->y,"
";

}

//析构函数

functionfunction __destruct()

{

echo "destruct function";

}

}

?>

抽象类

抽象类不能被实例化,用于为继承的子类定义接口,包含有属性和方法,其中必须有一个抽象方法。

继承

继承自抽象类的抽象方法,必须在子类中被重写。被重写的方法必须参数个数应该相同,可以有可选参数。

使用final修饰类,或者方法,这样就不能被继承或重写。

abstract class Object

{

abstract public function Output();

final public function Msg()

{

echo "object msg","
";

}

}

final class Point extends Object

{

public function Output()

{

//调用基类的方法,当然可以$this->Msg(),方法被覆盖时就不能用了吧!

parent::Msg();

echo "point","
";

}

}

class Rectangle extends Object

{

public function Output()

{

echo "Rectangle","
";

}

}

//多态性,限制传入的类型为Object

function runObject(Object $obj)

{

$obj->Output();

}

runObject(new Point());

runObject(new Rectangle());

?>

接口

interface IUser

{

public function addUser();

}

class Administrator implements IUser

{

public function addUser()

{

echo "add user";

}

}

(new Administrator())->addUser();

?>

内置对象函数

__autoload — 尝试加载未定义的类 | Attempt to load undefined class

call_user_method_array — 调用一个用户方法,同时传递参数数组(已废弃) | Call a user method given with an array of parameters

call_user_method — 对特定对象调用用户方法(已废弃) | Call a user method on an specific object

class_alias — 为一个类创建别名 | Creates an alias for a class

class_exists — 检查类是否已定义 | Checks if the class has been defined

get_called_class — 后期静态绑定("Late Static Binding")类的名称 | the "Late Static Binding" class name

get_class_methods — 返回由类的方法名组成的数组 | Gets the class methods' names

get_class_vars — 返回由类的默认属性组成的数组 | Get the default properties of the class

get_class — 返回对象的类名 | Returns the name of the class of an object

get_declared_classes — 返回由已定义类的名字所组成的数组 | Returns an array with the name of the defined classes

get_declared_interfaces — 返回一个数组包含所有已声明的接口 | Returns an array of all declared interfaces

get_declared_traits — 返回所有已定义的 traits 的数组 | Returns an array of all declared traits

get_object_vars — 返回由对象属性组成的关联数组 | Gets the properties of the given object

get_parent_class — 返回对象或类的父类名 | Retrieves the parent class name for object or class

interface_exists — 检查接口是否已被定义 | Checks if the interface has been defined

is_a — 如果对象属于该类或该类是此对象的父类则返回 TRUE | Checks if the object is of this class or has this class as one of its parents

is_subclass_of — 如果此对象是该类的子类,则返回 TRUE | Checks if the object has this class as one of its parents or implements it.

method_exists — 检查类的方法是否存在 | Checks if the class method exists

property_exists — 检查对象或类是否具有该属性 | Checks if the object or class has a property

trait_exists — 检查指定的 trait 是否存在 | Checks if the trait exists

评论:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值