Home Backend Development PHP Tutorial php面向对象编程(一)

php面向对象编程(一)

Jun 23, 2016 pm 01:16 PM

类与对象关系:

类就像一个人类的群体 我们从类中实例化一个对象 就像是制定一个人。

 面向对象程序的单位就是对象,但对象又是通过类的实例化出来的,所以我们首先要做的就是如何来声明类, 做出来一个类很容易。

类的格式

class 类名 { }

类可以理解为一个人的群体,如果我们要把这个人介绍给别人 那么

首先, 你会介绍这个人姓名、性别、年龄、身高、体重、电话、家庭住址等等。

然后,你要介绍这个人能做什么, 可以开车, 会说英语, 可以使用电脑等等。

从定义的角度可以分为:

1.静态的描述 如:人的姓名、性别、年龄、身高、体重、电话、家庭住址等等 我们将静态的描述称为成员属性

    我们可以用var 来定义 如 var $description ;此时我们应该注意的是声明没有必要赋值 ,如人的名字叫小明 有可能在这个人群中找到几十个小明

2.动态描述  如:这个人可以开车, 会说英语, 可以使用电脑等等   我们将动态的描述称为成员方法

class 人{	成员属性:姓名、性别、年龄、身高、体重、电话、家庭住址	成员方法:可以开车, 会说英语, 可以使用电脑}
Copy after login

<?phpclass Person{    // 下面是人的成员属性    var $name;    //人的名子    var $sex;    //人的性别    var $age;    //人的年龄    // 下面是人的成员方法    function say() // 这个人可以说话的方法    {         echo "这个人在说话";    }     function run() // 这个人可以走路的方法    {        echo "这个人在走路";    }}?>
Copy after login

当定义好一个类后 我们需要实例化那个类才能使用

用new的关键词 $对象名称 = new  类名称();

<?phpclass Person{    //下面是人的成员属性    var $name; //人的名子    var $sex; //人的性别    var $age; //人的年龄    //下面是人的成员方法    function say() { //这个人可以说话的方法        echo "这个人在说话";    }     function run() { //这个人可以走路的方法        echo "这个人在走路";    }}$p1=new Person();$p2=new Person();$p3=new Person();?>
Copy after login

  现在我们示例化类已经好了我们就要去学会如何使用类中的成员

对象->属性 $p1->name; $p2->age; $p3->sex;

对象->方法 $p1->say(); $p2->run()

<?phpclass Person{	// 下面是人的成员属性	var $name;		// 人的名子	var $sex;		// 人的性别	var $age;		// 人的年龄	// 下面是人的成员方法	function say() // 这个人可以说话的方法	{		echo "这个人在说话";	}	function run() // 这个人可以走路的方法	{		echo "这个人在走路";	}}$p1 = new Person(); //创建实例对象$p1$p2 = new Person(); //创建实例对象$p2$p3 = new Person(); //创建实例对象$p3// 下面三行是给$p1对象属性赋值$p1->name = "张三";$p1->sex = "男";$p1->age = 20;// 下面三行是访问$p1对象的属性echo "p1对象的名子是:" . $p1->name;echo "p1对象的性别是:" . $p1->sex;echo "p1对象的年龄是:" . $p1->age;// 下面两行访问$p1对象中的方法$p1->say();$p1->run();// 下面三行是给$p2对象属性赋值$p2->name = "李四";$p2->sex = "女";$p2->age = 30;// 下面三行是访问$p2对象的属性echo "p2对象的名子是:" . $p2->name;echo "p2对象的性别是:" . $p2->sex;echo "p2对象的年龄是:" . $p2->age;// 下面两行访问$p2对象中的方法$p2->say();$p2->run();// 下面三行是给$p3对象属性赋值$p3->name="王五";$p3->sex="男";$p3->age=40;// 下面三行是访问$p3对象的属性echo "p3对象的名子是:" . $p3->name;echo "p3对象的性别是:" . $p3->sex;echo "p3对象的年龄是:" . $p3->age;// 下面两行访问$p3对象中的方法$p3->say();$p3->run();?>
Copy after login

  此时类我们已经基本可以使用了 但是我们发现还是有点不足 就是我们可以在外部进行访问,但是不能在内部进行访问 我们引进一个概念“this”的用法 如果可以对内部进行访问赋值 那么将减少我们的代码量

  

<?phpclass Person{	// 下面是人的成员属性	var $name; //人的名子	var $sex; //人的性别	var $age; //人的年龄	// 下面是人的成员方法	function say() // 这个人可以说话的方法	{		echo "我的名子叫:" . $this->name . " 性别:" . $this->sex . " 我的年龄是:" . $this->age;	}	function run() // 这个人可以走路的方法	{		echo "这个人在走路";	}}$p1 = new Person(); // 创建实例对象$p1$p2 = new Person(); // 创建实例对象$p2$p3 = new Person(); // 创建实例对象$p3// 下面三行是给$p1对象属性赋值$p1->name = "张三";$p1->sex = "男";$p1->age = 20;// 下面访问$p1对象中的说话方法$p1->say();// 下面三行是给$p2对象属性赋值$p2->name = "李四";$p2->sex = "女";$p2->age = 30;// 下面访问$p2对象中的说话方法$p2->say();// 下面三行是给$p3对象属性赋值$p3->name = "王五";$p3->sex = "男";$p3->age = 40;// 下面访问$p3对象中的说话方法$p3->say();?>
Copy after login

  分析一下

function say() // 这个人可以说话的方法 {	echo "我的名子叫:" . $this->name . " 性别:" . $this->sex . " 我的年龄是:" . $this->age;}
Copy after login

  上面代码中$this指的是对其进行赋值的对象。

构造方法__construct()与析构方法__destruct()

构造方法__construct():即自动使用new的方法实现对象的实例化(可以理解为为实现能在实例化对象的时候传入参数构建一道桥梁)。理解为一个队列

  

<?// 创建一个人类class Person{	// 下面是人的成员属性	var $name;	// 人的名子	var $sex;	// 人的性别	var $age;	// 人的年龄	// 定义一个构造方法参数为姓名$name、性别$sex和年龄$age	function __construct($name, $sex, $age)	{		// 通过构造方法传进来的$name给成员属性$this->name赋初使值		$this->name = $name;		// 通过构造方法传进来的$sex给成员属性$this->sex赋初使值		$this->sex = $sex;		// 通过构造方法传进来的$age给成员属性$this->age赋初使值		$this->age = $age;	}	// 这个人的说话方法	function say()	{		echo "我的名子叫:" . $this->name . " 性别:" . $this->sex . " 我的年龄是:" . $this->age;	}}// 通过构造方法创建3个对象$p1、p2、$p3,分别传入三个不同的实参为姓名、性别和年龄$p1 = new Person("张三","男", 20);$p2 = new Person("李四","女", 30);$p3 = new Person("王五","男", 40);// 下面访问$p1对象中的说话方法$p1->say();// 下面访问$p2对象中的说话方法$p2->say();// 下面访问$p3对象中的说话方法$p3->say();?>
Copy after login

 

输出结果为:

我的名子叫:张三 性别:男 我的年龄是:20我的名子叫:李四 性别:女 我的年龄是:30我的名子叫:王五 性别:男 我的年龄是:40

 

析构方法__destruct():析构函数允许在销毁一个类之前进行一些列的操作 一个类的析构函数必须是__destruct() 注意的是析构函数不能附带任何的参数 理解为一个栈

  

<?// 创建一个人类class Person{	// 下面是人的成员属性	var $name;	// 人的名子	var $sex;	// 人的性别	var $age;	// 人的年龄	// 定义一个构造方法参数为姓名$name、性别$sex和年龄$age	function __construct($name, $sex, $age) 	{		// 通过构造方法传进来的$name给成员属性$this->name赋初使值		$this->name = $name;				// 通过构造方法传进来的$sex给成员属性$this->sex赋初使值		$this->sex = $sex;				// 通过构造方法传进来的$age给成员属性$this->age赋初使值		$this->age = $age;	}	// 这个人的说话方法	function say() 	{		echo "我的名子叫:" . $this->name . " 性别:" . $this->sex . " 我的年龄是:" . $this->age;	}	// 这是一个析构函数,在对象销毁前调用	function __destruct() 	{		echo "再见" . $this->name;	}}// 通过构造方法创建3个对象$p1、p2、$p3,分别传入三个不同的实参为姓名、性别和年龄$p1 = new Person("张三", "男", 20);$p2 = new Person("李四", "女", 30);$p3 = new Person("王五", "男", 40);// 下面访问$p1对象中的说话方法$p1->say();// 下面访问$p2对象中的说话方法$p2->say();// 下面访问$p3对象中的说话方法$p3->say();?>
Copy after login

  

输出结果为:

我的名子叫:张三 性别:男 我的年龄是:20我的名子叫:李四 性别:女 我的年龄是:30我的名子叫:王五 性别:男 我的年龄是:40
再见王五
再见李四
再见张三

 

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 尊渡假赌尊渡假赌尊渡假赌

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)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

PHP Logging: Best Practices for PHP Log Analysis PHP Logging: Best Practices for PHP Log Analysis Mar 10, 2025 pm 02:32 PM

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

See all articles