Home  >  Article  >  Backend Development  >  php object-oriented programming (1), php object-oriented programming (_PHP tutorial

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

WBOY
WBOYOriginal
2016-07-12 08:58:51876browse

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 人
{
	成员属性:姓名、性别、年龄、身高、体重、电话、家庭住址
	成员方法:可以开车, 会说英语, 可以使用电脑
}
<?<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>?>

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();
?>

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();
?>

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();
?>

Analyze it

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

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();
?>

The output result is:

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

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();
?>

 

The output result is:

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

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