Php面向对象 – 继承和重写
Php面向对象 – 继承和重写
Php面向对象 – 继承和重写
继承:
php中,通过在类上,使用特殊的操作达到目的。
通过在定义类时,利用extends来指明当前类对象继承那个类的对象。
例子:
class C
{
public $p_c = “value c”;
}
class D extends C
{
public $p_d = “value d”;
}
$o = new D;
var_dump($o->p_c)
var_dump($o->p_d)
输出:string(7) “value c” string(7)“value d”
继承,指的是两个对象之间,那么哪有这两个对象?
Instanceof 操作符 (判断对象是否是某类的实例)
var_dump($o instanceof D);
输出:bool(true)
var_dump($o instanceof C);
输出:bool(true)
因此,一个对象即是当前类的实例,也是该类所继承类的实例。
Class D extends C
D类对象,继承自C类对象。
父类:被继承的类,C类
子类:需要继承的类,D类
基类:C类是D类的基类
扩展类:D类是C类的扩展类。
重要:
php是单继承。
继承的目的:
在于扩展,或者使用某类已经存在的操作和数据。
重写 override
继承时,如果发生成员冲突,php的处理方式,为重写。就是子类同名成员覆盖父类同名成员。不能看到父类的同名成员。
例子:
1.
class P
{
public $name = ‘P’;
}
class C extends P
{
public $name = “C”;
}
$o = new C;
echo $o->name;
2.
class P
{
public $name = ‘P’;
public function sayName()
{
echo‘parent::name’,$this->name;
}
}
class C extends P
{
public $name = “C”;
public function sayName()
{
echo‘self::name’,$this->name;
}
}
$o = new C;
$o->sayName();
输出:self::name C
构造方法重写:
例子:
class P
{
public__construct()
{
echo“parent::construct”;
}
}
class D extends P
{
public__construct()
{
echo“self::construct”;
}
}
$o =new D;
输出:self::construct
如果需要,强制执行被重写的父类方法,可以显示的使用父类来调用相应的父类方法即可:
例子:
class P
{
public__construct()
{
echo“parent::construct”;
}
}
class D extends P
{
public__construct()
{
P::__construct();
echo“self::construct”;
}
}
$o =new D;
输出:parent::construct self::construct
可以使用一个关键字,在类内,代替当前的父类
parent关键字
例子:
class P
{
public__construct()
{
echo“parent::construct”;
}
}
class D extends P
{
public__construct()
{
parent::__construct();
echo“self::construct”;
}
}
$o =new D;
如果说父类的构造需要相应的参数,则需要在调用时,将父类构造方法需要的参数传递到方法内。
例子:
class Goods
{
public $goods_name ;
public $goods_price;
public function __construct($name,$price)
{
$this->goods_name= $name;
$this->goods_price= $price;
}
}
class GoodsBook extends Goods
{
public $pages;
public function __construct($name,$price,$pages)
{
parent::__construct($name,$price);
$this->pages= $pages;
}
}

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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

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

Use Python's __contains__() function to define the containment operation of an object. Python is a concise and powerful programming language that provides many powerful features to handle various types of data. One of them is to implement the containment operation of objects by defining the __contains__() function. This article will introduce how to use the __contains__() function to define the containment operation of an object, and give some sample code. The __contains__() function is Pytho

Here's how to convert a MySQL query result array into an object: Create an empty object array. Loop through the resulting array and create a new object for each row. Use a foreach loop to assign the key-value pairs of each row to the corresponding properties of the new object. Adds a new object to the object array. Close the database connection.

Wedge We know that objects are created in two main ways, one is through Python/CAPI, and the other is by calling a type object. For instance objects of built-in types, both methods are supported. For example, lists can be created through [] or list(). The former is Python/CAPI and the latter is a calling type object. But for instance objects of custom classes, we can only create them by calling type objects. If an object can be called, then the object is callable, otherwise it is not callable. Determining whether an object is callable depends on whether a method is defined in its corresponding type object. like

PHP functions can encapsulate data into a custom structure by returning an object using a return statement followed by an object instance. Syntax: functionget_object():object{}. This allows creating objects with custom properties and methods and processing data in the form of objects.

Title: Using Python's __le__() function to define a less than or equal comparison of two objects In Python, we can define comparison operations between objects by using special methods. One of them is the __le__() function, which is used to define less than or equal comparisons. The __le__() function is a magic method in Python and is a special function used to implement the "less than or equal" operation. When we compare two objects using the less than or equal operator (<=), Python

In PHP, an array is an ordered sequence, and elements are accessed by index; an object is an entity with properties and methods, created through the new keyword. Array access is via index, object access is via properties/methods. Array values are passed and object references are passed.

How to loop through Javascript objects? The following article will introduce five JS object traversal methods in detail, and briefly compare these five methods. I hope it will be helpful to you!
