Home Backend Development PHP Tutorial php面向对象详解

php面向对象详解

Jun 13, 2016 pm 01:27 PM
echo function info name

【分享】php面向对象详解

面向对象 对象概念是面向对象技术的核心。在显示世界里我们所面对的事情都是对象,如计算机、电视机、自行车等。在面向对象的程序设计中,对象是一个由信息及对信息进行处理的描述所组成的整体,是对现实世界的抽象.

对象的主要三个特性
对象的行为:可以对 对象施加那些操作,开灯,关灯就是行为。
对象的形态:当施加那些方法是对象如何响应,颜色,尺寸,外型。
对象的表示:对象的表示就相当于身份证,具体区分在相同的行为与状态下有什么不同。

面向对象模型

面向对象的概念:
oop(面向对象的编程)它能是其代码更加简洁易于维护并且具有更强的可重性

什么是类:
类是具有相同属性和服务的一组对象的集合比如说人,书,轮船,车都属于类,他为属于该类的对象做了一个统一的抽象描述,在编程的语言中类是一个单独的程序,它应该有一个类名包括属性的说明和服务两个部分。
什么是对象:
对象是系统中描述客观事件的一个实体,他是构成系统的一个基本单位。*数据与代码都被捆绑在一个实体当中*,一个对象由一组属性和对这组属性进行操作的一组行为组成。
从抽象的角度来说,对象是问题域或实现域中某些事物的一个抽象。他反映该事物在系统中保存的信息和发挥的作用:它是一组属性和有权对这些属性进行操作的一个封装体。客观世界是由对象和对象之间的联系组成的。
类和对象的关系:
类与对象的关系就如模具和铸件的关系,类的实力化的结果就是对象,而对对象的抽象就是类,类描述了一组有相同特性(属性)和相同行为的对象。

类与属性和方法
PHP中定义类语法格式:
class classname [可选属性]{
public $property [=value];… //用public声明一个公共标识 然后给予一个变量 变量也可以赋值
function functionname ( args ){ //类的方法里的成员函数
代码} …
//类的方法(成员函数)
}

生成对象(类的实例化): $对象名=new classname( );
使用对象的属性
在一个类中,可以访问一个特殊指针$this当在该类中通过一个操作设置或访问该变量时,使用$this->name来引用.
对象的生成
定义好类后用一个new来声明,由于对象资料的封装特性,对象是无法由主程序区块直接访问的须通过对象来调用类中所定义的属性和行为函数,间接地达成存取控制类中资料的目的。
对象和类的关系
对象和类的关系:
对象是实际存在的,占有动态资源。
类是对象的蓝图,可能占有静态资源。
对象属性占有动态资源
类(静态)属性实际上是有类名字空间上的“全局变量”
性能考虑:
每个对象要单独占用数据空间
增加的调用层次可能消耗执行时间
方法的参数形式和传递方式
方法的参数可以是基本数据类型、数组和类对象。
基本数据类型:值参传递
数组:值参传递
类对象:引用传递
构造函数
构造函数是在类中起到初始化的作用
构造函数的生成方法与其他函数一样只是其名称必须是__construct().
语法格式:function __construct(参数){
。。。。。。。。
}
范例:
class Person{
public $name;
public $sex;
public $age;
function __construct($name,$sex,$age){
echo "我是构造函数
";
$this->name=$name;
$this->sex=$sex;
$this->age=$age;
}
输出结果:初始化
析构函数
当对象脱离其作用域时(例如对象所在的函数已调用完毕),系统自动执行析构函数。应在退出前在析构函数中用释放内存。
析构函数__destruct 析构函数没有任何参数
范例:
class person{
function _ _destruct( )
{ echo "bye bye !“; }
}
$a=new person();

访问类型
public 公共的(公共修饰符) 类内部与类外部都可以访问的
private 私有的(私有修饰符) 只能在类内部访问
protected 受保护的(保护成员修饰符) 子类可以访问 类外部不可以访问

oop的三个重要特性
封装,继承,多态
封装性:封装性就是把对象的属性和行为结合成一个独立的单位。
封装一个类需要两步 第一步是私有化一个类 第二步是用set和get 做出读取赋值的操作
他的好处是:隐藏类的实现细节,可以方便加入逻辑控制性,限制对属性的不合理操作,便于修改增强代码的可维护性。

__get与__set
一般说把类私有话更符合现实的逻辑。
预定义两种函数来进行获取与敷值操作。
__get 获取值通常是域的值
__set 设置值通常是域的值
__call 调用一个对象中不存在的方法时,就会产生错误call()这个方法来处理这种情况。

静态属性和方法
static关键字 来声明静态方法
static静态变量 在类的内部生成一个静态变量 就是能够被所有类的实力化共想 也就是说静态成员则放到了“初始化静态段”,在类第一次被加载的时候放入的,可以让堆内存里面的每个对象所共享
使用方法:self::$静态属性、self::静态方法
static function p(){
echo self::$country;
echo self::PI;//访问常量
//echo $this->name;在静态方法中只能操作静态属性
//self::p();
}
外部调用:类::$静态属性、类::静态方法

const关键字:用来生成常量 常量是唯一的不能改变的 惯例常量为大写
const CONSTANT = 'constant value'; 生成一个常量
echo self::CONSTANT;//类内部访问
echo ClassName::CONSTANT;//类外部访问

继承性
B类的对象拥有A类的全部属性与行为,称作B对A类的继承。
假如一个类从多个类中继承了属性与服务,这称为多继承,通常我们成为继承类为子类被继承类为父类,在PHP中只有单继承,但一个父类可以被多个类继承,但是一个子类只能有一个父类,但是允许关联继承,通过继承可以减化类的定义。
extende声明继承关系
语法格式:class B extends A 此范例指明 B继承了A
类的外部访问对子类是有效的
子类与父类的属性与方法
子类继承父类的所有内容,但父类中的private部分不能直接访问
子类中新增加的属性和方法是对父类的扩展
子类中定义的与父类同名的属性是对父类属性的覆盖,同名的方法也是对父类方法的覆盖

重写的方法
在子类中,使用parent访问父类中的被覆盖的属性和方法
parent::__construce();
parent::$name;
parent::fun();

覆盖父类原有属性
clone克窿对象 语法格式$c=clone $p; $c克窿的对象$p 输出echo $c->name;

对象比较
===两个比较运算符。
==是比较两个对象的内容。
===是比较对象的句柄,即引用地址。

instanceof操作符用于检测对象实力是否属于某一个类的类型 属于返回true 不属于返回false
__clone()如果想在克隆后改变原对象的内容,需要在__clone()中重写原本的属性和方法
function __clone(){
$this->name="我是一个克隆人";
}

final表示一个类是最终版本 也就是说它不能在被子类调用

多态性

多态性是指在父类中定义的属性或行为被子类继承之后,可以具有不同的数据类型或表现出不同的行为。这使得同一个属性或行为在父类及其各个子类中具有不同的语义。
就是说同一种方法在子类与父类中执行的结果不同。
class A {
function info(){
echo “A INFO”;
}
}
class B extends A {
function info(){
echo “B INFO”;
}
}
class C extends A {
function info(){
echo “C INFO”;
}
}
function printinfo($obj){
function printinfo(A $obj){
if($obj instanceof A)
$obj->info();
$obj->info();
}
}
$a=new A(); $b=new B(); $c=new C();
printinfo($a); //输出A INFO
printinfo($b); //输出B INFO
printinfo($c); //输出C INFO

抽象方法和抽象类

抽象方法是作为子类摸版使用的。
abstract class Person{
public $name;
abstract function getInfo();
}
抽象类不能被实力话,一个抽象类中,必须有一个抽象方法。但是抽象类中可以定义动态函数。
接口
当一个类继承了一个接口之后,它要覆盖接口的所有方法,接口只能声明常量,接口的方法必须定义为共有否则无法继承,接口可以与多个接口间继承
语法:
interface PCI{
const TYPE="PCI";
//public $name; error
function start();
function stop();
}
接口中的方法可以声明为static
interface A{ function a();}
interface B{ function b();}
interface C extends A{ function c();}
class D implements B,C{
function a(){}
function b(){}
function c(){}
}

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

Video Face Swap

Video Face Swap

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

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)

What does function mean? What does function mean? Aug 04, 2023 am 10:33 AM

Function means function. It is a reusable code block with specific functions. It is one of the basic components of a program. It can accept input parameters, perform specific operations, and return results. Its purpose is to encapsulate a reusable block of code. code to improve code reusability and maintainability.

Five selected Go language open source projects to take you to explore the technology world Five selected Go language open source projects to take you to explore the technology world Jan 30, 2024 am 09:08 AM

In today's era of rapid technological development, programming languages ​​are springing up like mushrooms after a rain. One of the languages ​​that has attracted much attention is the Go language, which is loved by many developers for its simplicity, efficiency, concurrency safety and other features. The Go language is known for its strong ecosystem with many excellent open source projects. This article will introduce five selected Go language open source projects and lead readers to explore the world of Go language open source projects. KubernetesKubernetes is an open source container orchestration engine for automated

Go language development essentials: 5 popular framework recommendations Go language development essentials: 5 popular framework recommendations Mar 24, 2024 pm 01:15 PM

"Go Language Development Essentials: 5 Popular Framework Recommendations" As a fast and efficient programming language, Go language is favored by more and more developers. In order to improve development efficiency and optimize code structure, many developers choose to use frameworks to quickly build applications. In the world of Go language, there are many excellent frameworks to choose from. This article will introduce 5 popular Go language frameworks and provide specific code examples to help readers better understand and use these frameworks. 1.GinGin is a lightweight web framework with fast

Laravel development: How to implement WebSockets communication using Laravel Echo and Pusher? Laravel development: How to implement WebSockets communication using Laravel Echo and Pusher? Jun 13, 2023 pm 05:01 PM

Laravel is a popular PHP framework that is highly scalable and efficient. It provides many powerful tools and libraries that allow developers to quickly build high-quality web applications. Among them, LaravelEcho and Pusher are two very important tools through which WebSockets communication can be easily implemented. This article will detail how to use these two tools in Laravel applications. What are WebSockets? WebSockets

Detailed explanation of the role and usage of the echo keyword in PHP Detailed explanation of the role and usage of the echo keyword in PHP Jun 28, 2023 pm 08:12 PM

Detailed explanation of the role and usage of the echo keyword in PHP PHP is a widely used server-side scripting language, which is widely used in web development. The echo keyword is a method used to output content in PHP. This article will introduce in detail the function and use of the echo keyword. Function: The main function of the echo keyword is to output content to the browser. In web development, we need to dynamically present data to the front-end page. At this time, we can use the echo keyword to output the data to the page. e

What is the purpose of the 'enumerate()' function in Python? What is the purpose of the 'enumerate()' function in Python? Sep 01, 2023 am 11:29 AM

In this article, we will learn about enumerate() function and the purpose of “enumerate()” function in Python. What is the enumerate() function? Python's enumerate() function accepts a data collection as a parameter and returns an enumeration object. Enumeration objects are returned as key-value pairs. The key is the index corresponding to each item, and the value is the items. Syntax enumerate(iterable,start) Parameters iterable - The passed in data collection can be returned as an enumeration object, called iterablestart - As the name suggests, the starting index of the enumeration object is defined by start. if we ignore

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

What are the most popular golang frameworks on the market? What are the most popular golang frameworks on the market? Jun 01, 2024 pm 08:05 PM

The most popular Go frameworks at present are: Gin: lightweight, high-performance web framework, simple and easy to use. Echo: A fast, highly customizable web framework that provides high-performance routing and middleware. GorillaMux: A fast and flexible multiplexer that provides advanced routing configuration options. Fiber: A performance-optimized, high-performance web framework that handles high concurrent requests. Martini: A modular web framework with object-oriented design that provides a rich feature set.

See all articles