Home  >  Article  >  Backend Development  >  Detailed explanation of PHP (7) object-oriented programming_PHP tutorial

Detailed explanation of PHP (7) object-oriented programming_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:19:50887browse

Elaborate on php(7) object-oriented programming

1. Class declaration and object initialization

1.1 When declaring member attributes in a class: There must be a modifier in front of it. When you don’t know which one to use, use var. If you know which modifier keyword to use, don’t use var

var $color;

var $name = "zhangsan"


1.2 A file only saves one class, and the file name contains the class name, such as: class name.class.php

person.class.php


1.3 Use the new keyword to create an object. Once an object is created, a space is allocated in the memory. $Object reference = new class name;

$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();
?>
1.4 Allocation of objects in memory

a. Stack memory: stores local variables

b. Heap memory: storage object

c. Shared area: store static variables

d. Code segment: storage methods, etc.


2. Constructor and destructor

2.1 Constructor:

a. The constructor method is the first method that is automatically called after the object is created

b. In PHP4, the method with the same name as the class is the constructor

c. In PHP5, the constructor chooses to use the magic method __construct(). This name is used to declare constructors in all classes

Advantages: The constructor method does not need to be changed when changing the class name

d. The role of the constructor: initialize member properties

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

2.2 Destructor:

a. The destructor refers to the last method that is automatically called before the object is released

b. Like Java, PHP also uses a garbage collector to release resources, but PHP does so immediately after the call, while Java does not.

c. The role of the destructor: close some resources, do some cleanup work, use the magic method __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再见!
?>


2.3 Magic methods

Magic methods are methods provided by the system that are automatically called at different times to complete a certain function. Different magic methods have different calling times

Magic methods start with __

__construct(); // Constructor
__destruct(); // Destructor
__set();
__get();
__isset();
__unset();
__clone();
__call();
__sleep();
__weakup();
__toString()
__autoload();

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/871200.htmlTechArticleDetails on PHP (7) Object-oriented programming 1. Class declaration and object initialization 1.1 Declare member attributes in the class When: There must be a modifier in front of it. When you don’t know which one to use, use var, if...
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