Table of Contents
php object-oriented programming (1), php object-oriented programming (
Home Backend Development PHP Tutorial php object-oriented programming (1), php object-oriented programming (_PHP tutorial

php object-oriented programming (1), php object-oriented programming (_PHP tutorial

Jul 12, 2016 am 08:58 AM
Object-Oriented Programming

php object-oriented programming (1), php object-oriented programming (

Relationship between classes and objects:

A class is like a group of humans. Instantiating an object from a class is like specifying a person.

The unit of object-oriented program is the object, but the object is instantiated by the class, so the first thing we have to do is how to declare the class. It is easy to make a class.

Class format

class class name { }

A class can be understood as a group of people. If we want to introduce this person to others, then

First, you will introduce the person’s name, gender, age, height, weight, phone number, home address, etc.

Then, you have to introduce what this person can do, can drive, can speak English, can use a computer, etc.

From a definition point of view, it can be divided into:

1. Static description such as: person’s name, gender, age, height, weight, phone number, home address, etc. We call static description member attributes

We can use var to define such as var $description; at this time we should pay attention to the statement that there is no need to assign a value . For example, a person named Xiao Ming may be among this group of people. Found dozens of Xiaoming

2. Dynamic description For example: this person can drive, can speak English, can use a computer, etc. We call dynamic description member methods

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

After defining a class, we need to instantiate that class to use it

Use the new keyword $object name = new class name ();

<?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

Now that we have instantiated the class, we need to learn how to use the members in the class

Object->Properties $p1->name; $p2->age; $p3->sex;

Object->Method $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

At this time, we can basically use this class, but we found that there is still a shortcoming, that is, we can access it externally, but cannot Access it internally. We introduce the usage of a concept "this" If we can access it internally Performing access assignment internally will reduce the amount of our code

 

<?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

Analyze it

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

In the above code, $this refers to the object to which it is assigned.

Construction method __construct() and destructor method __destruct()

Construction method __construct(): automatically uses the new method to instantiate the object (which can be understood as building a bridge to pass in parameters when instantiates the object). Understood as a queue

 

<?
// 创建一个人类
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

The output result is:

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

Destructor method __destruct(): The destructor allows a series of operations to be performed before destroying a class. The destructor of a class must be __destruct(). Note that the destructor cannot have any parameters. It is understood as A stack

 

<?
// 创建一个人类
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

 

The output result is:

我的名子叫:张三 性别:男 我的年龄是:20我的名子叫:李四 性别:女 我的年龄是:30我的名子叫:王五 性别:男 我的年龄是:40<br />再见王五<br />再见李四<br />再见张三
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1101503.htmlTechArticlephp object-oriented programming (1), php object-oriented programming (relationship between classes and objects: A class is like a human Groups We instantiate an object from a class just like specifying a person...
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
1 months 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 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

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.

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

'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

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

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.

See all articles