PHP面向对象基础笔记一
难点:学习面向对象思想和语法,理解思想。 PHP5相对4添加了面向对象全部的基本语法。 一. 面向对象的编程思想 Object Oriented (1) 以事件为中心的编程思想 (2) 分析出解决问题的所需要的步骤 (3) 再用函数把步骤实现 (4) 使用的时候调用 二. 什
难点:学习面向对象思想和语法,理解思想。
PHP5相对4添加了面向对象全部的基本语法。
一. 面向对象的编程思想 Object Oriented
(1) 以事件为中心的编程思想
(2) 分析出解决问题的所需要的步骤
(3) 再用函数把步骤实现
(4) 使用的时候调用
二. 什么是对象
(1) 对象
表示具体的事物和抽象的规则、计划和事件。
(2) 状态和行为
用数据值来描述状态。
操作是对象的行为,用于改变对象的状态。
对象实现数据和操作的结合,将数据和对象封装于对象。
三. 什么是面向对象
面向对象是以功能划分问题,而不是步骤。
注重的是对象,和对象的使用
只有面向对象的程序,才真正的符合人类的思维逻辑。
四. 面向对象的重要优势
可读性、可重用性、稳定性、维护性、可测试性。
五. 类和对象之间的关系
对象通过类的描述生成的。对象的属性是在类中已经写好的。
同一类中每个对象是相互独立的。
六. 类的声明
1. 简单格式:
[修饰符] class 类名{ // 使用class关键字加空格后加上类名
[成员属性] //也叫成员变量
[成员方法] //也叫成员函数
}
2. 完整格式:
[修饰符] class 类名 [extends父类][implements接口1],[接口2]..{
[成员属性] //也叫成员变量
[成员方法] //也叫成员函数
}
3. 格式
修饰符$变量名[=默认值];//如:public $name = “zhangsan”;
注意:成员属性不可以是带运算符的表达式、变量、方法、或者是函数调用。
正确的定义方式:
常用的属性修饰符:public、private、protected、static、var(过时)
一. 成员方法
1. 成员方法格式:
[修饰符]function 方法名(参数..){
[方法体]
[return返回值]
}
2. 修饰符:public、protected、private、static、abstract、final
3. 声明的成员方法必须和对象相关,不能是一些没有意义的操作。
完整案例:

八. 封装
封装性就是把对象中的成员属性和成员方法加上修饰符,尽可能隐藏对象的内部细节,以达到对成员的访问控制(切记不是拒绝访问)
案例:

九 . 魔术方法
1. __get()
自动调用:是在直接访问私有成员时,自动调用!
不是私有属性不调用,可以将属性名称传给参数。
这样每个属性返回的都是名字,输出如下:
或者:
输出如下:
2. __set()
自动调用:是在直接设置私有属性时,两个参数。一个属性名,一个属性值。
输出结果:
3. __isset()
自动调用:是在使用issset()判断一个私有属性是否存在时,自动调用__isset()魔术方法,参数则是属性名称。
4. __unset()

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

AI Hentai Generator
Generate AI Hentai for free.

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



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Validator can be created by adding the following two lines in the controller.

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.
