左手发麻什么原因| 蒲公英有什么作用| 脾虚不能吃什么| 俗不可耐什么意思| 姑奶奶的老公叫什么| 壬申日是什么意思| 妾是什么意思| 碧霄是什么意思| 一个雨一个亏念什么| 言重了是什么意思| 十一月二十五是什么星座| 梦见大棺材是什么预兆| 9月1号什么星座| 鹿象征什么寓意| 例假一个月来两次是什么原因| 小孩抽多动症吃什么药最好| 狗懒子是什么意思| 一颗颗什么| 月亮星座代表什么意思| 什么是高热量食物有哪些| 什么食物热量低| ella是什么意思| 骨质增生是什么| 小孩上火吃什么药| 头发软化和拉直有什么区别| 臀疗是什么| 和女生聊天聊什么| bpc是什么意思| 黄糖是什么糖| 水牛是什么意思| 芥菜是什么菜| 奥倍健是什么药| 烧烤用什么油| 损友是什么意思| exo的e为什么不发音| 做绝育手术对女人有什么影响| 较真的人是什么性格| 肺炎为什么要7到10天才能好| 老是放屁什么原因| 蚊子怕什么气味| 沈阳六院主要治什么病| 徘徊什么意思| 大雄宝殿供奉的是什么佛| 狗狗中毒了用什么办法可以解毒| 菠菜是什么意思| 社保断了有什么影响| 十一月九号是什么星座| 双侧甲状腺弥漫病变是什么意思| asc是什么意思| 9月14号是什么星座| 刚愎自用代表什么生肖| 孔雀为什么会开屏| 为什么打哈欠会流泪| 菠菜不能和什么食物一起吃| 杜鹃花什么时候开花| 燊念什么| 紧急避孕药什么时候吃最好| 12月6日什么星座| 人生三件大事是指什么| 内分泌紊乱是什么症状| 赏脸是什么意思| 肛门瘙痒用什么药最好| 什么食物铅含量高| 什么人容易得胆结石| TA什么意思| 四川酸菜是什么菜| 什么是夜店| 子宫偏小是什么原因| 减脂吃什么主食| 纯牛奶什么时候喝最好| 附子理中丸治什么病| 梦见狗吃屎是什么意思| 初音未来是什么| 富贵病是什么病| 舌头发热是什么原因| 22年属什么生肖| 农转非是什么意思| 今年33岁属什么生肖的| 过什么意思| 仁字五行属什么| 蜂蜜有什么作用| 4.22是什么日子| 电子商务学什么| 备孕喝苏打水什么作用| 为什么不能抠肚脐眼| 奶茶喝多了有什么危害| 板栗不能和什么一起吃| 老母鸡炖什么好吃又有营养价值| 黄疸是什么意思| 宝宝头爱出汗是什么原因| 胃食管反流挂什么科| size是什么意思| 陈皮有什么功效| 虹为什么是虫字旁| 起风疹的原因是什么引起的| 吃什么变碱性体质最快| 脑供血不足什么原因引起的| 打感情牌是什么意思| ys是什么意思| 作陪是什么意思| 什么是妇科病| 正常白带是什么样的| 怀孕前三个月忌吃什么| 启攒是什么意思| 便秘有什么症状| 男生进入是什么感觉| 鲁迅的原名叫什么| 鲁迅是著名的什么家| bnp是什么检查| 盂是什么意思| 五味子有什么作用| 有什么不能说| 什么的雨| 多吃鱼有什么好处| 一致是什么意思| 同房后需要注意什么| 封神是什么意思| 长生香是什么意思| 淇字五行属什么| 脸麻手麻是什么原因| 趋是什么意思| 灵魂伴侣是什么意思| 帕金森是什么原因引起的| 什么时候买机票便宜| 做梦梦见出车祸是什么征兆| 长期不过性生活有什么危害| 输卵管发炎有什么症状表现| o型血可以接受什么血型| 经信局是做什么的| 阴道口有点痒用什么药| 肾衰竭是什么意思| 脖子上长痘痘是什么原因| 大是大非是什么意思| 无机磷测定是检查什么| 二月二十三日是什么星座| 梦遗是什么原因| 天为什么会下雨| 厦门有什么区| 冰毒是什么| 6月份是什么星座的| 什么是植发| 出生证编号是什么| 什么精什么神| 脾虚吃什么中药| 挑食是什么意思| exr是什么牌子| 小酌怡情什么意思| 为什么身上有红色的痣| 除了肠镜还有什么方法检查肠道| 尿液茶色是什么原因| 彪子是什么意思| 粉色象征着什么| 装模作样是什么生肖| 医学美容技术学什么| 智商是什么| 金国人是现在的什么人| 辐照食品什么意思| 土字旁的字与什么有关| 前途是什么意思| wt是什么意思| 枫叶是什么颜色的| 漱口杯什么材质好| 阴道里面长什么样| 心包积液是什么意思| 考护师需要什么条件| zgo手表是什么牌子| 废话是什么意思| 吃恩替卡韦有什么副作用| 什么睡姿对髋关节好| 男人脚肿是什么原因| 阿莫西林有什么作用| 隆胸有什么危害和后遗症吗| 什么是畸胎瘤| 鬼节为什么不能出去| 着凉感冒吃什么药| 柠檬黄配什么颜色好看| 什么鱼没刺| 潜意识是什么意思| 上火流鼻血吃什么降火| 宗师是什么意思| 大名鼎鼎是什么意思| 被弹颏是什么意思| 取向是什么意思| 为什么一直打喷嚏| 38妇女节送老婆什么礼物| 咳嗽出血是什么原因| 岁岁年年是什么意思| 猫咪弓背是什么原因| 立加羽念什么| 红肉指的是什么肉| viki什么意思| c反应蛋白高吃什么药| 比重是什么意思| 糖尿病是什么症状| 舌头裂纹是什么病| 人体自由基是什么| 吉兰巴雷综合征是什么病| 二甲双胍是什么药| 队友是什么意思| 吃鹅蛋对孕妇有什么好处| 拔智齿需要注意什么| 四季更迭是什么意思| 腱鞘炎去医院挂什么科| 上号是什么意思| 8.1号是什么星座| 霉菌性阴道炎是什么症状| 什么什么本本| 朱元璋为什么要杀刘伯温| 象牙有什么作用与功效| 肺结节不能吃什么食物| 大便粘稠是什么原因| 载脂蛋白b偏低是什么意思| 检验科是做什么的| 良善是什么意思| 发呆表情是什么意思| chick什么意思| 动脉圆锥是什么意思| 可乐饼为什么叫可乐饼| 乙肝前s1抗原阳性是什么意思| 外阴瘙痒用什么药好| 风热感冒吃什么食物| 哮喘有什么症状| 高湛为什么帮梅长苏| 随笔是什么意思| 胎儿为什么会喜欢臀位| 北京属于什么方向| 桡神经受损有什么恢复的方法| 胸部疼痛挂什么科| 犬字旁的字和什么有关| 硒中毒有什么症状| 什么东西化痰效果最好最快| 珩字五行属什么| 麦冬是什么| 晚上喝什么有助于睡眠| 血压高可以吃什么水果| 男人左手断掌是什么命| 咖啡是什么做的| 女性尿路感染用什么药| 飞机是什么意思| 蝶变是什么意思| 奶酪是什么做的| 射手座的幸运色是什么| 什么时候做人流才是最佳时间| 很困但是睡不着是什么原因| 肌腱炎有什么症状| 红细胞偏低有什么危害| 关我什么事| 长命的动物是什么生肖| 多囊为什么要跳绳而不是跑步| 有妇之夫是什么意思| 汉语什么意思| 补铁的药什么时候吃最好| 骨刺吃什么药| 白细胞低代表什么意思| 梦见家里水管漏水是什么意思| 陨石有什么作用和功效| 什么能增强免疫力| 补骨脂是什么东西| 月亮发红是什么原因| 中指麻木是什么原因引起的| 梦到被蛇咬是什么预兆| 必要条件是什么意思| 老鼠疣长什么样子图片| 日行一善下一句是什么| 百度
PHP object-orie...LOGIN

男子南京南站被挤压致死案立案 家属索赔80万元

百度 聚丙烯是其中最常见的微粒。

In object-oriented programming (English: Object-oriented programming, abbreviation: OOP), an object is a whole composed of information and a description of how to process the information. It is an abstraction of the real world.

In the real world, the things we face are objects, such as computers, televisions, bicycles, etc.


The main three characteristics of the object:

· The behavior of the object: what operations can be applied to the object, turn on the light, turn off the light The light is the action.

· The shape of the object: How the object responds when those methods are applied, color, size, appearance.

· Representation of objects: The representation of objects is equivalent to an ID card. It specifically distinguishes the differences between the same behaviors and states.

For example, Animal is an abstract class. We can specify a dog and a sheep, and dogs and sheep are concrete objects. They have color attributes, can be written, can run and other behavioral states. .

3.png

Object-oriented content

· Class ? defines the abstract characteristics of a thing. The definition of a class includes the form of the data and the operations on the data.

· Object ? is an instance of a class.

· Member variables ? Variables defined inside the class. The value of this variable is invisible to the outside world, but can be accessed through member functions. After the class is instantiated as an object, the variable can be called an attribute of the object.

· Member function ? Defined inside the class, it can be used to access the data of the object.

· Inheritance ? Inheritance is a mechanism for subclasses to automatically share the data structures and methods of parent classes. This is a relationship between classes. When defining and implementing a class, you can do it on the basis of an existing class, take the content defined by the existing class as your own content, and add some new content.

· Parent class ? A class is inherited by other classes. This class can be called a parent class, a base class, or a super class.

· Subclass ? A class that inherits other classes is called a subclass, or it can also be called a derived class.

· Polymorphism - Polymorphism means that the same operation, function, or process can be applied to multiple types of objects and obtain different results. Different objects can produce different results when receiving the same message. This phenomenon is called polymorphism.

· Overloading - Simply put, it is a situation where functions or methods have the same name but different parameter lists. Such functions or methods with the same name and different parameters are called overloaded functions or methods. method.

· Abstraction ? Abstraction refers to abstracting objects with consistent data structures (attributes) and behaviors (operations) into classes. A class is an abstraction that reflects important properties related to an application while ignoring other irrelevant content. The division of any class is subjective, but must be related to the specific application.

· Encapsulation ? Encapsulation refers to binding the attributes and behaviors of an object existing in the real world and placing them in a logical unit.

· Constructor ? Mainly used to initialize the object when creating the object, that is, assign initial values ??to the object member variables. It is always used together with the new operator in the statement to create the object.

· Destructor ? Destructor (destructor) Contrary to the constructor, when the object ends its life cycle (for example, the function in which the object is located has been called), the system automatically executes the destructor. Destructors are often used to do "clean-up" work (for example, when creating an object, use new to open up a memory space, which should be released with delete in the destructor before exiting).

In the figure below, we have created three objects through the Car class: Mercedes, Bmw, and Audi.

4.png

$mercedes = new Car ();

$bmw = new Car ();

$audi = new Car () ;


PHP class definition

##PHP The usual syntax format for defining a class is as follows:

<?php
 class phpClass {
     var $var1;
     var $var2 = "constant string";
 
     function myfunc ($arg1, $arg2) {
         [..]
   }
 [..]
 }
 ?>

is parsed as follows:

· Class usage Add the class name definition after the class keyword.

· Variables and methods can be defined within a pair of braces ({}) after the class name.

· Class variables are declared using var, and variables can also be initialized.

· Function definition is similar to PHP function definition, but functions can only be accessed through the class and its instantiated objects.

Instance

<?php
 class Site {
     /* 成员变量 */
     var $url;
     var $title;
 
     /* 成员函数 */
     function setUrl($par){
         $this->url = $par;
     }
 
     function getUrl(){
         echo $this->url ."<br/>";
     }
 
     function setTitle($par){
         $this->title = $par;
     }
 
     function getTitle(){
         echo $this->title . "<br/>";
     }
 }?>

##Variable $this represents its own object.


Creating objects in PHPAfter a class is created, we can use the new operator to instantiate objects of this class:

$php = new Site;

$taobao = new Site;

$google = new Site;

In the above code, we created three objects, each of which is independent. Next, let’s take a look at how to access member methods and member variables.

// Call the member function and set the title and URL

$php->setTitle( "php中文网" );
$taobao-> setTitle( "Taobao" );
$google->setTitle( "Google Search" );

$php->setUrl( 'www.php.cn' );
$taobao ->setUrl( 'www.taobao.com' );
$google->setUrl( 'www.google.com' );

// Call the member function to get the title and URL
$php->getTitle();
$taobao->getTitle();
$google->getTitle();

$php->getUrl();
$taobao->getUrl();
$google->getUrl();

We combine the codes we learned above:

##The complete code is as follows:

<?php
 header("Content-type:text/html;charset=utf-8");    //设置编码
 class Site {
     /* 成员变量 */
     var $url;
     var $title;
 
     /* 成员函数 */
     function setUrl($par){
         $this->url = $par;
     }
 
     function getUrl(){
         echo $this->url ."<br/>";
     }
 
     function setTitle($par){
         $this->title = $par;
     }
 
     function getTitle(){
         echo $this->title . "<br/>";
     }
 }
 
 $php = new Site;
 $taobao = new Site;
 $google = new Site;
 
 // 调用成员函数,设置标题和URL
 $php->setTitle( "php中文网" );
 $taobao->setTitle( "淘宝" );
 $google->setTitle( "Google 搜索" );
 
 $php->setUrl( 'www.php.cn' );
 $taobao->setUrl( 'www.taobao.com' );
 $google->setUrl( 'www.google.com' );
 
 // 调用成员函数,获取标题和URL
 $php->getTitle();
 $taobao->getTitle();
 $google->getTitle();
 
 $php->getUrl();
 $taobao->getUrl();
 $google->getUrl();
 ?>

Program running result:

php中文网

Taobao
Google search
www.php.cn
www.taobao.com
www .google.com


##PHP Constructor

Constructor is a special method. It is mainly used to initialize the object when creating the object, that is, to assign initial values ??to the object member variables. It is always used together with the new operator in the statement to create the object.

PHP 5 allows developers to define a method as a constructor in a class. The syntax format is as follows:

##void __construct ([ mixed $args [, $... ]] ) In the above example, we can initialize $url and $title through the construction method Variable:

function __construct( $par1, $par2 ) {

$this->url = $par1;

$ this->title = $par2;

}

Now we no longer need to call the setTitle and setUrl methods:

Example

Use the constructor to simplify the above code:

<?php
 header("Content-type:text/html;charset=utf-8");    //设置编码
 class Site {
     /* 成员变量 */
     var $url;
     var $title;
 
     /* 成员函数 */
     function setUrl($par){
         $this->url = $par;
     }
 
     function getUrl(){
         echo $this->url ."<br/>";
     }
 
     function setTitle($par){
         $this->title = $par;
     }
 
     function getTitle(){
         echo $this->title . "<br/>";
     }
     function __construct( $par1, $par2 ) {
         $this->url = $par1;
         $this->title = $par2;
     }
 }
 
 // 调用成员函数,设置标题和URL
 $php = new Site('www.php.cn', 'php中文网');
 $taobao = new Site('www.taobao.com', '淘宝');
 $google = new Site('www.google.com', 'Google 搜索');
 
 // 调用成员函数,获取标题和URL
 $php->getTitle();
 $taobao->getTitle();
 $google->getTitle();
 
 $php->getUrl();
 $taobao->getUrl();
 $google->getUrl();
 ?>

Program running results:

php中文网
Taobao
Google Search
www.php.cn
www.taobao.com
www.google.com


## Analysis Constructor

Destructor (destructor) Contrary to the constructor, when the object ends its life cycle (such as the function in which the object is located has been called), the system automatically executes the destructor.

PHP 5 introduced the concept of destructor, which is similar to other object-oriented languages. Its syntax format is as follows:

void __destruct (void)

Example

<?php
 header("Content-type:text/html;charset=utf-8");    //设置编码
 class MyDestructableClass {
     function __construct() {
         print "构造函数\n";
         $this->name = "MyDestructableClass";
     }
 
     function __destruct() {
         print "销毁 " . $this->name . "\n";
     }
 }
 
 $obj = new MyDestructableClass();
 ?>

Program running result:

Constructor Destroy MyDestructableClass


##InheritancePHP uses the keyword extends to inherit a class. PHP does not support multiple inheritance. Format As follows:

class Child extends Parent {

// Code part

}

above This means that the Child class inherits the Parent class

InstanceIn the following example, the Child_Site class inherits the Site class and extends the functionality:

<?php
// 子类扩展站点类别
class Child_Site extends Site {
    var $category;
    function setCate($par){
        $this->category = $par;
    }
    function getCate(){
        echo $this->category . "<br/>";
    }
}
?>


Method rewritingIf the method inherited from the parent class cannot meet the needs of the subclass, it can be rewritten. This process is called method override, also known as method rewriting.

The following example rewrites the getUrl and getTitle methods:

function getUrl() {
echo $this->url . PHP_EOL;
return $this->url;
}

function getTitle(){
echo $this->title . PHP_EOL;
return $this->title;
}


##Access Control

PHP access control to properties or methods is achieved by adding the keywords public (public), protected (protected) or private (private) in front.

·

public (public): Public class members can be accessed from anywhere.

·

protected: Protected class members can be accessed by itself and its subclasses and parent classes.

·

private: Private class members can only be accessed by the class in which they are defined.

Access control of attributes

Class attributes must be defined as one of public, protected, and private. If defined with var, it is considered public.

Example

<?php
 /**
  * Define MyClass
  */
 class MyClass
 {
     public $public = 'Public';
     protected $protected = 'Protected';
     private $private = 'Private';
 
     function printHello()
     {
         echo $this->public;
         echo $this->protected;
         echo $this->private;
     }
 }
 
 $obj = new MyClass();
 echo $obj->public; // 这行能被正常执行
 echo $obj->protected; // 这行会产生一个致命错误
 echo $obj->private; // 这行也会产生一个致命错误
 $obj->printHello(); // 输出 Public、Protected 和 Private
 
 
 /**
  * Define MyClass2
  */
 class MyClass2 extends MyClass
 {
     // 可以对 public 和 protected 进行重定义,但 private 而不能
     protected $protected = 'Protected2';
 
     function printHello()
     {
         echo $this->public;
         echo $this->protected;
         echo $this->private;
     }
 }
 
 $obj2 = new MyClass2();
 echo $obj2->public; // 这行能被正常执行
 echo $obj2->private; // 未定义 private
 echo $obj2->protected; // 这行会产生一个致命错误
 $obj2->printHello(); // 输出 Public、Protected2 和 Undefined
 
 ?>

Look carefully at the above code


Method access control

Methods in a class can be defined as public, private or protected. If these keywords are not set, the method defaults to public.

Instance

<?php
 /**
  * Define MyClass
  */
 class MyClass
 {
     // 声明一个公有的构造函数
     public function __construct() { }
 
     // 声明一个公有的方法
     public function MyPublic() { }
 
     // 声明一个受保护的方法
     protected function MyProtected() { }
 
     // 声明一个私有的方法
     private function MyPrivate() { }
 
     // 此方法为公有
     function Foo()
     {
         $this->MyPublic();
         $this->MyProtected();
         $this->MyPrivate();
     }
 }
 
 $myclass = new MyClass;
 $myclass->MyPublic(); // 这行能被正常执行
 $myclass->MyProtected(); // 这行会产生一个致命错误
 $myclass->MyPrivate(); // 这行会产生一个致命错误
 $myclass->Foo(); // 公有,受保护,私有都可以执行
 
 
 /**
  * Define MyClass2
  */
 class MyClass2 extends MyClass
 {
     // 此方法为公有
     function Foo2()
     {
         $this->MyPublic();
         $this->MyProtected();
         $this->MyPrivate(); // 这行会产生一个致命错误
     }
 }
 
 $myclass2 = new MyClass2;
 $myclass2->MyPublic(); // 这行能被正常执行
 $myclass2->Foo2(); // 公有的和受保护的都可执行,但私有的不行
 
 class Bar
 {
     public function test() {
         $this->testPrivate();
         $this->testPublic();
     }
 
     public function testPublic() {
         echo "Bar::testPublic\n";
     }
 
     private function testPrivate() {
         echo "Bar::testPrivate\n";
     }
 }
 
 class Foo extends Bar
 {
     public function testPublic() {
         echo "Foo::testPublic\n";
     }
 
     private function testPrivate() {
         echo "Foo::testPrivate\n";
     }
 }
 
 $myFoo = new foo();
 $myFoo->test(); // Bar::testPrivate
 // Foo::testPublic
 ?>


Interface

Use interface (interface), you can specify which methods a certain class must implement, but you do not need to define the specific content of these methods.

The interface is defined through the interface keyword, just like defining a standard class, but all methods defined in it are empty.

All methods defined in the interface must be public. This is a characteristic of the interface.

To implement an interface, use the implements operator. The class must implement all methods defined in the interface, otherwise a fatal error will be reported. A class can implement multiple interfaces. Use commas to separate the names of multiple interfaces.

Instance

<?php
// 声明一个'iTemplate'接口
interface iTemplate
{
    public function setVariable($name, $var);
    public function getHtml($template);
}
// 实现接口
class Template implements iTemplate
{
    private $vars = array();
    public function setVariable($name, $var)
    {
        $this->vars[$name] = $var;
    }
    public function getHtml($template)
    {
        foreach($this->vars as $name => $value) {
            $template = str_replace('{' . $name . '}', $value, $template);
        }
        return $template;
    }
}
?>


##Constant

You can define values ??that remain unchanged in the class as constants. There is no need to use the $ symbol when defining and using constants.

The value of a constant must be a fixed value and cannot be a variable, class attribute, the result of a mathematical operation or a function call.

Since PHP 5.3.0, you can use a variable to dynamically call a class. But the value of this variable cannot be a keyword (such as self, parent or static).

Instance

<?php
 class MyClass
 {
     const constant = '常量值';
 
     function showConstant() {
         echo  self::constant . "<br/>";
     }
 }
 
 echo MyClass::constant . "<br/>";
 
 $classname = "MyClass";
 echo $classname::constant . "<br/>"; // 自 5.3.0 起
 
 $class = new MyClass();
 $class->showConstant();
 
 echo $class::constant . "<br/>"; // 自 PHP 5.3.0 起
 ?>

Abstract class

Any A class must be declared abstract if at least one method in it is declared abstract.

Classes defined as abstract cannot be instantiated.

A method defined as abstract only declares its calling method (parameters) and cannot define its specific function implementation.

When inheriting an abstract class, the subclass must define all abstract methods in the parent class; in addition, the access control of these methods must be the same (or more relaxed) as in the parent class. For example, if an abstract method is declared as protected, then the method implemented in the

subclass should be declared as protected or public, and cannot be defined as private. In addition, the method calling method must match, that is, the type and number of required parameters must be consistent. For example, if the subclass defines an optional parameter

, but it is not included in the declaration of the abstract method of the parent class, there is no conflict between the two declarations.

Example

<?php
class ConcreteClass1 extends AbstractClass
{
    protected function getValue() {
        return "ConcreteClass1";
    }
    public function prefixValue($prefix) {
        return "{$prefix}ConcreteClass1";
    }
}
class ConcreteClass2 extends AbstractClass
{
    public function getValue() {
        return "ConcreteClass2";
    }
    public function prefixValue($prefix) {
        return "{$prefix}ConcreteClass2";
    }
}
$class1 = new ConcreteClass1;
$class1->printOut();
echo $class1->prefixValue('FOO_') . "<br/>";
$class2 = new ConcreteClass2;
$class2->printOut();
echo $class2->prefixValue('FOO_') . "<br/>";
?>

Program running result:

ConcreteClass1
FOO_ConcreteClass1
ConcreteClass2
FOO_ConcreteClass2


Static keyword

declares a class attribute or method as static (static), you can access it directly without instantiating the class.

Static properties cannot be accessed through an object of a class that has been instantiated (but static methods can).

Since static methods do not require an object to be called, the pseudo variable $this is not available in static methods.

Static properties cannot be accessed by objects through the -> operator.

Since PHP 5.3.0, you can use a variable to dynamically call a class. But the value of this variable cannot be the keywords self, parent or static.

Example

<?php
class Foo {
public static $my_static = 'foo';
public function staticValue() {
return self::$my_static;
}
}
print Foo::$my_static . "<br/>";
$foo = new Foo();
print $foo->staticValue() . "<br/>";
?>

Program running result:

foo
foo


Final Keyword

PHP 5 adds a new final keyword. If a method in the parent class is declared final, the child class cannot override the method. If a class is declared final, it cannot be inherited.

Example

An error will be reported when executing the following code:

<?php
 class BaseClass {
     public function test() {
         echo "BaseClass::test() called" . PHP_EOL;
     }
 
     final public function moreTesting() {
         echo "BaseClass::moreTesting() called"  . PHP_EOL;
     }
 }
 
 class ChildClass extends BaseClass {
     public function moreTesting() {
         echo "ChildClass::moreTesting() called"  . PHP_EOL;
     }
 }
 // 报错信息 Fatal error: Cannot override final method BaseClass::moreTesting()
 ?>

Program running result:

Fatal error: Cannot override final method BaseClass::moreTesting() in D:\WWW\Basis\oop\opp_9.php on line 16


Call parent Class constructor method

PHP will not automatically call the constructor of the parent class in the constructor of the subclass. To execute the constructor of the parent class, you need to call parent::__construct()

Instance

<?php
 header("Content-type:text/html;charset=utf-8");    //设置编码
 class BaseClass {
     function __construct() {
         print "BaseClass 类中构造方法" . "<br/>";
     }
 }
 class SubClass extends BaseClass {
     function __construct() {
         parent::__construct();  // 子类构造方法不能自动调用父类的构造方法
         print "SubClass 类中构造方法" . "<br/>";
     }
 }
 class OtherSubClass extends BaseClass {
     // 继承 BaseClass 的构造方法
 }
 
 // 调用 BaseClass 构造方法
 $obj = new BaseClass();
 
 // 调用 BaseClass、SubClass 构造方法
 $obj = new SubClass();
 
 // 调用 BaseClass 构造方法
 $obj = new OtherSubClass();
 ?>

# in the constructor of the subclass ##Program running results:

Construction method in BaseClass class

Construction method in BaseClass class
Construction method in SubClass class
Construction method in BaseClass class


Next Section

<?php header("Content-type:text/html;charset=utf-8"); //设置编码 class Site { /* 成员变量 */ var $url; var $title; /* 成员函数 */ function setUrl($par){ $this->url = $par; } function getUrl(){ echo $this->url ."<br/>"; } function setTitle($par){ $this->title = $par; } function getTitle(){ echo $this->title . "<br/>"; } } $php = new Site; $taobao = new Site; $google = new Site; // 调用成员函数,设置标题和URL $php->setTitle( "php中文网" ); $taobao->setTitle( "淘宝" ); $google->setTitle( "Google 搜索" ); $php->setUrl( 'www.php.cn' ); $taobao->setUrl( 'www.taobao.com' ); $google->setUrl( 'www.google.com' ); // 调用成员函数,获取标题和URL $php->getTitle(); $taobao->getTitle(); $google->getTitle(); $php->getUrl(); $taobao->getUrl(); $google->getUrl(); ?>
submitReset Code
ChapterCourseware
坐月子哭了会有什么后遗症 总是口渴是什么原因 月经时间长是什么原因 小孩子经常流鼻血是什么原因 眩晕是什么症状
五险一金包括什么 医保统筹是什么意思 什么品种的榴莲最好吃 蟑螂讨厌什么味道 怀孕初期吃什么
高血压适合吃什么食物 脂蛋白是什么 什么河水 jeep是什么意思 loves是什么意思
比目鱼又叫什么鱼 总蛋白偏低是什么意思 鱼加完念什么 海胆是什么动物 小t是什么意思
马刺是什么意思hcv8jop5ns9r.cn 鼻子出血是什么原因hcv8jop5ns9r.cn 小便尿出乳白色液体是什么问题hcv9jop3ns2r.cn 偏瘫是什么意思hcv8jop8ns7r.cn 颐养天年是什么意思hcv8jop1ns8r.cn
强是什么生肖hcv8jop5ns2r.cn 包皮过长有什么危害hcv8jop0ns7r.cn 眼皮重是什么原因hcv8jop9ns2r.cn 直率是什么意思creativexi.com 2005年是什么生肖hcv9jop1ns9r.cn
啪啪是什么意思hcv8jop2ns6r.cn u什么意思aiwuzhiyu.com 小孩子腿疼是什么原因hcv7jop6ns6r.cn 口腔溃疡吃什么药好的快hcv8jop6ns9r.cn 手心发热什么原因hcv8jop6ns7r.cn
孕吐喝什么水可以缓解hcv8jop0ns7r.cn 到底为了什么baiqunet.com 吃什么对心脏有好处hcv8jop1ns0r.cn 怀孕10天左右有什么症状hcv9jop1ns9r.cn 卫衣是什么hcv8jop0ns3r.cn
百度