Table of Contents
php面向对象编程(一),php面向对象编程(
Home php教程 php手册 php面向对象编程(一),php面向对象编程(

php面向对象编程(一),php面向对象编程(

Jun 13, 2016 am 08:45 AM
Object-Oriented Programming

php面向对象编程(一),php面向对象编程(

类与对象关系:

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

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

类的格式

class 类名 { }

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

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

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

从定义的角度可以分为:

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

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

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

class 人
{
	成员属性:姓名、性别、年龄、身高、体重、电话、家庭住址
	成员方法:可以开车, 会说英语, 可以使用电脑
}
Copy after login
<?<span>php
</span><span>class</span><span> Person
{
    </span><span>//</span><span> 下面是人的成员属性</span>
    <span>var</span> <span>$name</span>;    <span>//</span><span>人的名子</span>
    <span>var</span> <span>$sex</span>;    <span>//</span><span>人的性别</span>
    <span>var</span> <span>$age</span>;    <span>//</span><span>人的年龄

    // 下面是人的成员方法</span>
    <span>function</span> say() <span>//</span><span> 这个人可以说话的方法</span>
<span>    { 
        </span><span>echo</span> "这个人在说话"<span>;
    } 

    </span><span>function</span> run() <span>//</span><span> 这个人可以走路的方法</span>
<span>    {
        </span><span>echo</span> "这个人在走路"<span>;
    }
}
</span>?>
Copy after login

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

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

<?php
class 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()

<?php
class 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”的用法 如果可以对内部进行访问赋值 那么将减少我们的代码量

  

<?php
class 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
Copy after login

 

析构方法__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<br />再见王五<br />再见李四<br />再见张三
Copy after login

 

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)

PHP MVC Architecture: Building Web Applications for the Future PHP MVC Architecture: Building Web Applications for the Future Mar 03, 2024 am 09:01 AM

Introduction In today's rapidly evolving digital world, it is crucial to build robust, flexible and maintainable WEB applications. The PHPmvc architecture provides an ideal solution to achieve this goal. MVC (Model-View-Controller) is a widely used design pattern that separates various aspects of an application into independent components. The foundation of MVC architecture The core principle of MVC architecture is separation of concerns: Model: encapsulates the data and business logic of the application. View: Responsible for presenting data and handling user interaction. Controller: Coordinates the interaction between models and views, manages user requests and business logic. PHPMVC Architecture The phpMVC architecture follows the traditional MVC pattern, but also introduces language-specific features. The following is PHPMVC

'PHP Object-Oriented Programming Design Patterns: Understanding SOLID Principles and Their Applications' 'PHP Object-Oriented Programming Design Patterns: Understanding SOLID Principles and Their Applications' Feb 25, 2024 pm 09:20 PM

SOLID principles are a set of guiding principles in object-oriented programming design patterns that aim to improve the quality and maintainability of software design. Proposed by Robert C. Martin, SOLID principles include: Single Responsibility Principle (SRP): A class should be responsible for only one task, and this task should be encapsulated in the class. This can improve the maintainability and reusability of the class. classUser{private$id;private$name;private$email;publicfunction__construct($id,$nam

PHP extension development: How to design custom functions to support object-oriented programming? PHP extension development: How to design custom functions to support object-oriented programming? Jun 01, 2024 pm 03:40 PM

PHP extensions can support object-oriented programming by designing custom functions to create objects, access properties, and call methods. First create a custom function to instantiate the object, and then define functions that get properties and call methods. In actual combat, we can customize the function to create a MyClass object, obtain its my_property attribute, and call its my_method method.

PHP's object-oriented programming paradigm offers advantages to project management and organizations PHP's object-oriented programming paradigm offers advantages to project management and organizations Sep 08, 2023 am 08:15 AM

PHP's object-oriented programming paradigm provides advantages for project management and organization With the rapid development of the Internet, websites and applications of all sizes have sprung up. In order to meet the growing needs and improve development efficiency and maintainability, the use of object-oriented programming (Object-Oriented Programming, OOP for short) has become the mainstream of modern software development. In dynamic scripting languages ​​like PHP, OOP brings many advantages to project management and organization. This article will introduce

Application of golang functions in high concurrency scenarios in object-oriented programming Application of golang functions in high concurrency scenarios in object-oriented programming Apr 30, 2024 pm 01:33 PM

In high-concurrency scenarios of object-oriented programming, functions are widely used in the Go language: Functions as methods: Functions can be attached to structures to implement object-oriented programming, conveniently operating structure data and providing specific functions. Functions as concurrent execution bodies: Functions can be used as goroutine execution bodies to implement concurrent task execution and improve program efficiency. Function as callback: Functions can be passed as parameters to other functions and be called when specific events or operations occur, providing a flexible callback mechanism.

'Introduction to Object-Oriented Programming in PHP: From Concept to Practice' 'Introduction to Object-Oriented Programming in PHP: From Concept to Practice' Feb 25, 2024 pm 09:04 PM

What is object-oriented programming? Object-oriented programming (OOP) is a programming paradigm that abstracts real-world entities into classes and uses objects to represent these entities. Classes define the properties and behavior of objects, and objects instantiate classes. The main advantage of OOP is that it makes code easier to understand, maintain and reuse. Basic Concepts of OOP The main concepts of OOP include classes, objects, properties and methods. A class is the blueprint of an object, which defines its properties and behavior. An object is an instance of a class and has all the properties and behaviors of the class. Properties are characteristics of an object that can store data. Methods are functions of an object that can operate on the object's data. Advantages of OOP The main advantages of OOP include: Reusability: OOP can make the code more

How do C++ functions differ from object-oriented programming? How do C++ functions differ from object-oriented programming? Apr 11, 2024 pm 09:12 PM

Functional and Object-Oriented Programming (OOP) provide different programming mechanisms in C++: Function: independent block of code, focused on performing a specific task, containing no data. OOP: Based on objects, classes and inheritance, data and behavior are encapsulated in objects. In actual cases, the function method for calculating the area of ​​a square is simple and direct, while the OOP method encapsulates data and behavior and is more suitable for managing object interaction. Choosing the appropriate approach depends on the scenario: functions are good for independent tasks, OOP is good for managing complex object interactions.

Python entry to proficiency: from zero basics to project development Python entry to proficiency: from zero basics to project development Feb 20, 2024 am 11:42 AM

1. Introduction to Python Python is a general-purpose programming language that is easy to learn and powerful. It was created by Guido van Rossum in 1991. Python's design philosophy emphasizes code readability and provides developers with rich libraries and tools to help them build various applications quickly and efficiently. 2. Python basic syntax The basic syntax of Python is similar to other programming languages, including variables, data types, operators, control flow statements, etc. Variables are used to store data. Data types define the data types that variables can store. Operators are used to perform various operations on data. Control flow statements are used to control the execution flow of the program. 3.Python data types in Python

See all articles