php面向对象开发学习笔记
面向对象程序设计(英语:Object-oriented programming,缩写:OOP),指一种程序设计范型,同时也是一种程序开发的方法。对象指的是类的集合。它将对象作为程序的基本单元,将程序和数据封装其中,以提高软件的重用性、灵活性和扩展性
注意:
1.在定义对象方法的时候,虽然不用在前面写public默认为公共方法,但是建议写上。
php面向对象第一天
1.什么是面向对象?
要素:抽象性,封装性,共享性,强调对象结构而不是程序结构。
什么是类?
可以理解成一个功能集合菜单,我们通过类来实现生成我们的方法。
例:一台电脑
类:显示器/键盘/主机...的集合(封装)。
对象:显示器/键盘/主机……其中之一,拥有自己的独有功能。
属性:电脑。
方法:提高功能,看电影,玩游戏,编程,上网……。
2.使用class创建一个类
有了类才能有方法。
格式:
class 方法名 {
......
}
成员属性:自定义变量(一个名而已)。
成员方法:自定义功能。
3.类的成员属性和成员方法
注意:使用public来定义成员属性不要用var。
实例:
代码如下 | 复制代码 |
class MyPc { //声明一个类,定义一个方法MyPc。 public $name; //成员属性。 var $price; //成员属性 function vod() { //成员方法,实现返回字符串功能。 return "test"; } } |
4.使用new函数对象实例化
格式:new 对象名(参数)
实例1:
代码如下 | 复制代码 |
class MyPc { //声明一个类,定义一个方法MyPc。 $pc1 = new Mypc(); //实例化 |
实例2:
代码如下 | 复制代码 |
class MyPc { var $key; function vod() { } $pc1 = new MyPc(); echo $pc1->key;
|
php面向对象第二天
1.创建一个和多个对象。
2.对象中的 $this 关键字。
3.初始化对象 __construct()
4.析构函数 __destruct()
-----------------------------------------
1.创建一个和多个对象
创建一个和多个对象我们只需要多次使用 new 函数,对类进行实例化。
实例:
代码如下 | 复制代码 |
class MyPc { function vod() { $pc1 = new MyPc();
|
2.对象中的 $this 关键字
$this 关键字是用来访问当前对象中的对象属性和对象方法的系统变量。
范围:该类当中。
实例:
代码如下 | 复制代码 |
class MyPc { public $name;-------- public $price; | | function vod() { | $this->name; } .... $this->vod(); //这样输出调用的就是 public $name; .... } |
实例2:
代码如下 | 复制代码 |
class MyPc { function vod() { } $pc1->name = "1号计算机";
|
3.初始化对象 __construct()
初始化相当于就是预先预定好某个成员属性的初始值。
格式:
class MyPc {
function __construct() {
初始化操作
}
}
实例:
代码如下 | 复制代码 |
class MyPc { function __construct($name = "") { //初始化。 function vod() { function game() { } echo $pc1->vod() . " |
4.析构函数 __destruct()
能够在对象释放时自动被调用的方法,被成为析构函数,也能理解为垃圾回收机制。
规则:后进先出,先实例化后释放,最后实例化调用,最先释放。
最后被调用。
实例:
代码如下 | 复制代码 |
class MyPc { function __construct($name = "") { function vod() { function game() { function __destruct() { //后进先出。 } echo $pc1->vod() . " |
php面向对象第三天
类的封装和应用
1.封装关键字:public , protected , private。
2.封装相关函数:__set() , __get()。
-----------------------------------------------
1.封装关键字:public , protected , private
封装是把一些相关的属性和行为隐藏起来,从而得到保护和安全。
封装关键字
public:表示全局,类内部外部子类都可以访问。
protected:表示受保护的,只有本类或子类或父类中可以访问。
private:表示私有的,只有本类内部可以使用。[重要:这个关键字在使用时,调用私有属性或方法,只能在本类调用,感觉就像多一道工序,做一个跳板。详见实例2]
public protected private
全局 1 0 0
继承类 1 1 0
本类 1 1 1
实例:
代码如下 | 复制代码 |
class MyPc { private function vod() { //私有属性 |
实例2:
代码如下 | 复制代码 |
class MyPc {
|
分装相关函数: __set() , __get() 作用:操作私有属性或方法。
__set():取得当前类中封装过的私有属性或私有方法,重新执行或赋值操作。
格式:__set($n,$v)
__get():取得(间接访问防止裸输出)当前类中封装过的属性或方法并转换成公有属性。
__get/__set实例:
代码如下 | 复制代码 |
class MyPc { public function __get($name) { |
类的继承和应用
1.继承关键字:extends。
2.PHP继承的规则。
3.基类方法重载和基类方法访问。
------------------------------------------------
1.继承关键字:extends
PHP类的继承,我们可以理解成共享被继承类(基类)的类容。
注意:PHP是单一继承。
2.格式
代码如下 | 复制代码 |
class MyPc { class Home extends MyPc { |
3.基类方法重载和基类方法访问
格式:基类名::原始基类方法名称
4.实例
继承:
代码如下 | 复制代码 |
class Root { class Son extends Root { $p = new Son(); |
重载:
如果基类的某个方法需要在派生类中加强,那么可以用重载功能
代码如下 | 复制代码 |
class Root { class Son extends Root { $p = new Son();
|
类的抽象方法和类
其实也可以理解为,这是一个规范。在类的开头定义一个抽象类和方法,然后在下面的类继承抽象类,这样可以强制规范以下派生类的方法命名(就只是在抽象类中定义的抽象方法名,还可自己增加,但是主要的不能修改)。
1.抽象关键字:abstract。
2.抽象方法和抽象类的定义。
3.抽象类和方法使用规则。
-----------------------------------------
1.抽象关键字:abstract
抽象就是无法确切的说明,但又有一定的概念或者名称。
2.抽象方法和抽象类的定义
一个类至少有一个方法是抽象的,我们称之为抽象类。
所以如果定义抽象类首先定义抽象方法。
格式:
abstract class class1 {
abstract function fun1();
...
}
注意:1.类中至少有一个抽象方法。;2.抽象方法不允许有{}。;3.抽象方法前面必须加 abstract 。
3.抽象类和方法使用规则
抽象类特点:
1.不能被实例化,只能被继承。
2.继承的派生类当中要把所有抽象方法重载才能被实例化。
格式(不可以实例化):
abstract class cl1 {
abstract function fun1();
...
}
格式(可以实例化):
class cl2 extends cl1 {
function fun1() { //重载抽象方法
}
...
}
---------------------------------------------------
实例:
代码如下 | 复制代码 |
abstract class ChouXiang { } class PaiShengLei extends ChouXiang { $p = new PaiShengLei(); |

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

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

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 and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.
