Home Backend Development PHP Tutorial PHP面向对象概念_PHP

PHP面向对象概念_PHP

Jun 01, 2016 pm 12:13 PM
php object-oriented

关键字和特殊变量

new,class,extends。这三个,大家都懂得。
::,范围解析操作符(也可称作 Paamayim Nekudotayim)或者更简单地说是一对冒号,可以用于访问静态成员、方法和常量。还可以用于覆盖类中的成员和方法。
parent和self。parent指的就是派生类在 extends 声明中所指的基类的名字。这样做可以避免在多个地方使用基类的名字。
$this 伪变量。$this指向当前的实例。$this却不一定是方法所属的对象。有时候类A内的代码会调用类B的一个静态方法。参考例子:http://www.php.net/manual/zh/language.oop5.basic.php
static关键字。如果声明类成员或方法为static,就可以不实例化类而直接访问。但是,除了静态方法,不能通过一个对象来访问其中的静态成员。在静态方法中,不使用$this。而使用self:: 。
final关键字。可以作用于类(class)和方法(function),会使类不能被继承,方法不能被覆盖。

属性

  可以初始化,但初始化的值必须是常数。常量前面用const个关键字,常量的值必须是一个定值,不能是变量,类属性或其它操作(如函数调用)的结果。

构造函数和析构函数

  这两个函数都不会暗中调用基类的响应函数,这跟java的构造器机制不一样。要达到这样的效果,必须显示执行。析构函数中不能抛出异常。

抽象类:声明为抽象的类方法不能包含具体实现,抽象类不能实例化。必须先被继承,再实例化其子类。而且子类的访问控制要和抽象类一样,或者更加宽松。抽象类至少包含一个抽象方法的。

接口

使用接口(interface),你可以指定某个类必须实现哪些方法,但不需要定义这些方法的具体内容。
定义的所有方法都必须是public,且方法为空
可以定义常量,但是没有属性
接口的实现(implements)必须实现所有方法,而且可以实现多个接口(注意,方法不能重名)。
接口可以被另外的接口继承 (extends)
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

An in-depth explanation of PHP's object-oriented encapsulation An in-depth explanation of PHP's object-oriented encapsulation Aug 11, 2023 am 11:00 AM

In-depth interpretation of PHP object-oriented encapsulation Encapsulation is one of the three major characteristics of object-oriented programming. It refers to encapsulating data and operations on data in a class, hiding specific implementation details from external programs, and providing external interfaces. In PHP, the concept of encapsulation is implemented by using access modifiers (public, protected, private) to control the accessibility of properties and methods. First, let’s take a look at the role of access modifiers: public (public): Public properties and methods can

How to implement object version control and management through PHP object-oriented simple factory pattern How to implement object version control and management through PHP object-oriented simple factory pattern Sep 06, 2023 pm 02:39 PM

How to implement object version control and management through the PHP object-oriented simple factory model. When developing large and complex PHP projects, version control and management are very important. Through appropriate design patterns, we can better manage and control the creation and use of objects, thereby improving the maintainability and scalability of the code. This article will introduce how to use the PHP object-oriented simple factory pattern to implement object version control and management. The simple factory pattern is a design pattern for creating classes that instantiates specified objects through a factory class

How to master object-oriented programming skills in PHP development How to master object-oriented programming skills in PHP development Jun 25, 2023 am 08:05 AM

With the development of the Internet, PHP has gradually become one of the most popular programming languages ​​​​in web development. However, with the rapid development of PHP, object-oriented programming has become one of the necessary skills in PHP development. In this article, we will discuss how to master object-oriented programming skills in PHP development. Understanding the Concepts of Object-Oriented Programming Object-oriented programming is a programming paradigm that organizes code and data through the use of objects (classes, properties, and methods). In object-oriented programming, code is organized into reusable modules, thereby improving the program's

How to create flexible object instances using PHP object-oriented simple factory pattern How to create flexible object instances using PHP object-oriented simple factory pattern Sep 06, 2023 pm 02:12 PM

How to create flexible object instances using the PHP object-oriented simple factory pattern. The simple factory pattern is a common design pattern that creates object instances without exposing object creation logic. This mode can improve the flexibility and maintainability of the code, and is especially suitable for scenarios where different objects need to be dynamically created based on input conditions. In PHP, we can use the characteristics of object-oriented programming to implement the simple factory pattern. Let's look at an example below. Suppose we need to create a graphing calculator that can

Understand the object-oriented inheritance mechanism of PHP Understand the object-oriented inheritance mechanism of PHP Aug 10, 2023 am 10:40 AM

Understanding the Object-Oriented Inheritance Mechanism of PHP Inheritance is an important concept in object-oriented programming, which allows the creation of new classes that include the features and functionality of the old classes. In PHP, inheritance can be achieved through the keyword extends. Through inheritance, subclasses can inherit the properties and methods of the parent class, and can add new properties and methods, or override inherited methods. Let us understand the object-oriented inheritance mechanism of PHP through an example. classAnimal{public$name

Detailed explanation of common problems in PHP object-oriented programming Detailed explanation of common problems in PHP object-oriented programming Jun 09, 2023 am 09:27 AM

PHP language has become a very popular web development language because it is easy to learn and use. Object-oriented programming is one of the most important programming paradigms in the PHP language. However, object-oriented programming is not something that is easy to master, so some common problems often arise. In this article, we will provide a detailed analysis of common problems with object-oriented programming in PHP. Question 1: How to create an object? In PHP, you can use the new keyword to create an object. For example: classMyClass{/

How to achieve seamless switching and replacement of objects through PHP object-oriented simple factory pattern How to achieve seamless switching and replacement of objects through PHP object-oriented simple factory pattern Sep 06, 2023 am 08:01 AM

How to achieve seamless switching and replacement of objects through PHP's object-oriented simple factory pattern Introduction: In PHP development, object-oriented programming (Object-oriented Programming, referred to as OOP) is a very common programming paradigm. The object-oriented design pattern can further improve the maintainability and scalability of the code. This article will focus on the simple factory pattern in PHP to achieve seamless switching and replacement of objects. What is the simple factory pattern? Simple factory pattern (Simple

How PHP implements object-oriented programming and improves code readability and maintainability How PHP implements object-oriented programming and improves code readability and maintainability Jun 27, 2023 pm 12:28 PM

With the continuous development of Internet technology, PHP has become one of our common website development languages, and PHP object-oriented programming has also become a knowledge point that must be learned. Object-oriented programming (OOP) is a programming paradigm whose core concept is to combine data and behavior into an object to improve code reusability, readability, and maintainability. This article will explore how to use PHP to implement object-oriented programming and improve the readability and maintainability of your code. Basic Concepts of Object-Oriented Programming In object-oriented programming, each object has a set of properties.

See all articles