Table of Contents
细说php(七) 面向对象编程
Home php教程 php手册 细说php(七) 面向对象编程

细说php(七) 面向对象编程

Jun 13, 2016 am 09:26 AM
object

细说php(七) 面向对象编程

一、类的声明与对象初始化

1.1 在类中声明成员属性时: 前面必须有修饰词, 当不知道使用那个时, 就使用var, 如果知道使用那一个修饰关键字, 就不使用var了

var $color;

var $name = "zhangsan"


1.2 一个文件只保存一个类, 文件名中包含类名, 如:类名.class.php

person.class.php


1.3 使用new关键字来创建对象, 创建了一个对象就在内存中分配一个空间 $对象引用 = new 类名;

$person = new Person

<?php
	class Person {
		var $name;      // Java: private String name;
		var $age;
		var $sex;
	
		function say() {
			echo $this->name;
		}
	}
	
	$p1 = new Person;   // Java: Person person = new Person;
	$p1->name = "lisi"; // Java: person.name = "lisi";
	$p1->say();         // Java: person.say();
?>
Copy after login
1.4 对象在内存中的分配

a. 栈内存: 存放局部变量

b. 堆内存: 存放对象

c. 共享区: 存放静态变量

d. 代码段: 存放方法等


二、构造函数和析构函数

2.1 构造函数:

a. 构造方法是对象创建完成之后, 第一个自动调用的方法

b. 在PHP4中, 和类同名的方法就是构造方法

c. 在PHP5中, 构造方法选择使用魔术方法 __construct() , 所有类中声明构造方法都使用这个名称

优点: 在改变类名时构造方法不用改变

d. 构造方法的作用: 为成员属性初始化

<?php
	class Person {
		var $name;
		var $age;
		var $sex;
	
		function __construct($name="", $age=0, $sex="男"){
			$this->name=$name;
			$this->age=$age;
			$this->sex=$sex;
		}
	
		function say(){
			echo "我的名子:{$this->name},我的年龄:{$this->age},我的性别:{$this->sex}。<br>";
		}
	}
	
	$p1=new Person("zhangsan", 20, "女");
	$p2=new Person("lisi", 25);
	$p3=new Person("wangwu");
	
	$p1->say();
	$p2->say();
	$p3->say();
?>
Copy after login

2.2 析构函数:

a. 析构函数是指当对象被释放之前最后一个自动调用的方法

b. 和Java一样, PHP也使用垃圾回收器释放资源, 只不过PHP调用后马上回收, 而Java不是.

c. 析构函数的作用: 关闭一些资源, 做一些清理工作, 使用魔术方法 __destruct()

<?php
	class Person {
		var $name;
		var $age;
		var $sex;
	
		function __construct($name="", $age=0, $sex="男"){
			$this->name=$name;
			$this->age=$age;
			$this->sex=$sex;
		}
	
		function say(){
			echo "我的名子:{$this->name},我的年龄:{$this->age},我的性别:{$this->sex}。<br>";
		}
		
		function __destruct(){
			echo $this->name."再见!<br>";
		}
	}
	
	$p1=new Person("zhangsan", 20, "女");
	$p1->say();
	$p1 = null;
	
	// 我的名子:zhangsan,我的年龄:20,我的性别:女。
    // zhangsan再见!
?>
Copy after login


2.3 魔术方法

魔术方法是系统给我们提供好的, 在不同时刻为完成某一功能而自动调用的方法, 不同的魔术方法有不同的调用时机

魔术方法以 __ 开头

__construct(); // 构造函数
__destruct(); // 析构函数
__set();
__get();
__isset();
__unset();
__clone();
__call();
__sleep();
__weakup();
__toString()
__autoload();

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 Article Tags

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)

Convert an array or object to a JSON string using PHP's json_encode() function Convert an array or object to a JSON string using PHP's json_encode() function Nov 03, 2023 pm 03:30 PM

Convert an array or object to a JSON string using PHP's json_encode() function

Source code exploration: How are objects called in Python? Source code exploration: How are objects called in Python? May 11, 2023 am 11:46 AM

Source code exploration: How are objects called in Python?

What is the Request object in PHP? What is the Request object in PHP? Feb 27, 2024 pm 09:06 PM

What is the Request object in PHP?

How to convert MySQL query result array to object? How to convert MySQL query result array to object? Apr 29, 2024 pm 01:09 PM

How to convert MySQL query result array to object?

Use Python's __contains__() function to define the containment operation of an object Use Python's __contains__() function to define the containment operation of an object Aug 22, 2023 pm 04:23 PM

Use Python's __contains__() function to define the containment operation of an object

Use Python's __le__() function to define a less than or equal comparison of two objects Use Python's __le__() function to define a less than or equal comparison of two objects Aug 21, 2023 pm 09:29 PM

Use Python's __le__() function to define a less than or equal comparison of two objects

What is the difference between arrays and objects in PHP? What is the difference between arrays and objects in PHP? Apr 29, 2024 pm 02:39 PM

What is the difference between arrays and objects in PHP?

Detailed explanation of 5 loop traversal methods of Javascript objects Detailed explanation of 5 loop traversal methods of Javascript objects Aug 04, 2022 pm 05:28 PM

Detailed explanation of 5 loop traversal methods of Javascript objects

See all articles