Home php教程 php手册 PHP面向对象基础笔记一

PHP面向对象基础笔记一

Jun 06, 2016 pm 07:59 PM
php Base study object Thought notes difficulty For

难点:学习面向对象思想和语法,理解思想。 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”;

注意:成员属性不可以是带运算符的表达式、变量、方法、或者是函数调用。


PHP面向对象基础笔记一

正确的定义方式:


PHP面向对象基础笔记一

常用的属性修饰符:public、private、protected、static、var(过时)

一. 成员方法

1. 成员方法格式:

[修饰符]function 方法名(参数..){

   [方法体]

   [return返回值]

}

2. 修饰符:public、protected、private、static、abstract、final

3. 声明的成员方法必须和对象相关,不能是一些没有意义的操作。

完整案例:

PHP面向对象基础笔记一

八. 封装

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

PHP面向对象基础笔记一

案例:


PHP面向对象基础笔记一


九 .  魔术方法

1. __get()

 自动调用:是在直接访问私有成员时,自动调用!

不是私有属性不调用,可以将属性名称传给参数。


PHP面向对象基础笔记一

PHP面向对象基础笔记一


这样每个属性返回的都是名字,输出如下:


PHP面向对象基础笔记一

或者:


PHP面向对象基础笔记一

输出如下:


PHP面向对象基础笔记一


2. __set()

自动调用:是在直接设置私有属性时,两个参数。一个属性名,一个属性值。


PHP面向对象基础笔记一

PHP面向对象基础笔记一

输出结果:


PHP面向对象基础笔记一

3. __isset()

自动调用:是在使用issset()判断一个私有属性是否存在时,自动调用__isset()魔术方法,参数则是属性名称。

4. __unset()


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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

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

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

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

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

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

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

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

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

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

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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 Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

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.

See all articles