1. Rules of class
- You can only put one class in a file (required)
- Name of file and class name (required)
- Class files end with.class.php (not required)
1.1 Manually Loading classes
In project development, since only one class can be written in a file, and many classes will participate in the execution process, we first demonstrate the manual loading of classes one by one. The project structure is as follows:
PHP ├ ─ ─ the Book.classIndex.php ├ ─ ─ Goods.class.php
├── index.php
├── Phone.class.php
Copy the code
Book.class.php ();
/ / class books
class Book extends Goods {
public function getName () {
echo ""{$this->name}"< br / >"; }}? >
Copy the code
Class.php ();
/ / class goods
abstract class Goods {
protected $name;
final public function setName ($name) {
$this->name = $name;
}
public abstract function getName();
}
? >
Copy the code
Index. PHP:
require './Goods.class.php';
require './Book.class.php';
require './Phone.class.php';
/ / test
$book = new Book();
$book -> setName('Object-oriented Programming');
$phone = new Phone();
$phone -> setName('huawei');
$book -> getName();
$phone -> getName();
? >
Copy the code
Phones.class.php ();
/ / phone
class Phone extends Goods {
public function getName () {
echo $this->name,'<br/>'; }}? >
Copy the code
The running results are as follows:As you can see, it is very troublesome for us to manually load classes one by one. The following is an introduction to automatic class loading.
1.2 Automatically loading classes
In project development, only one class can be written in a file, and there will be many classes involved in the execution process. If one class is loaded one by one, it is very troublesome. Therefore, a mechanism is needed to automatically load the required classes during the execution of PHP.
1.2.1 __autoload () function
Method 1: the __autoload() function automatically calls the __autoload() function when there is a missing class, passing the missing class name as an argument to __autoload().
The project structure is as follows:
PHP ├ ─ ─ the Book.classIndex.php ├ ─ ─ Goods.class.php
├── index.php
├── Phone.class.php
Copy the code
Change the contents of index.php as follows (nothing else is changed) :
// Automatically load the class
function __autoload ($class_name) {
require ". /{$class_name}.class.php";
}
/ / test
$book = new Book();
$book -> setName('Object-oriented Programming');
$phone = new Phone();
$phone -> setName('huawei');
$book -> getName();
$phone -> getName();
? >
Copy the code
The effect is as follows:
You can see that we use auto-loading classes, which can be handy. Note, however: __autoload() is not supported after PHP7.2.
1.2.2 spl_autoload_register ()
Register the __autoload() function and the project structure remains the same. Index. PHP:
/ / method
function loadClass ($class_name) {
require ". /{$class_name}.class.php";
}
// Register the loading class function
spl_autoload_register('loadClass');
/ / method 2
// spl_autoload_register(function ($class_name) {
// require "./{$class_name}.class.php";
// });
/ / test
$book = new Book();
$book -> setName('Object-oriented Programming');
$phone = new Phone();
$phone -> setName('huawei');
$book -> getName();
$phone -> getName();
? >
Copy the code
Effect:It is also possible to register multiple functions (in the form of a queue, first in, first out) to load different classes.
1.3 Irregular storage of class files
Class files store irregular loading methods that map class names and file addresses to form an associative array. The code is as follows:
spl_autoload_register(function ($class_name) {
// Class names and file addresses are mapped into an associative array
$map = array(
'Goods'= >'./aa/Goods.class.php'.'Book'= >'./bb/Book.class.php'.'Phone'= >'./cc/Phone.class.php'
);
// Include it when found in the mapping array
if (isset($map[$class_name]) {require $map[$class_name]; }})/ / test
$book = new Book();
$book -> setName('Object-oriented Programming');
$phone = new Phone();
$phone -> setName('huawei');
$book -> getName();
$phone -> getName();
? >
Copy the code
In the project, the vast majority of the storage is regular, irregular is less.
2, __clone()
What are the ways to create objects? Method 1: Instantiate method 2: clone
class Student {}$stu1=new Student;
$stu2=new Student;
var_dump($stu1.$stu2);
? >
Copy the code
Effect:
class Student {}$stu1=new Student;
$stu2=$stu1;
var_dump($stu1.$stu2);
? >
Copy the code
Effect:
class Student {}$stu1=new Student;
$stu2=clone $stu1;
var_dump($stu1.$stu2);
? >
Copy the code
Effect:
The __clone() function is automatically called when the Clone instruction is called. Examples:
class Student {
public function __clone() {
echo 'Cloning'; }}$stu1=new Student;
$stu2=clone $stu1;
var_dump($stu1.$stu2);
? >
Copy the code
Effect:2. When executing the Clone instruction, __clone() method will be automatically called
3. Design pattern
3.1 Singleton mode
A class can have only one object. Application scenario: A database with multiple requests requires only one connection object. Realization: three private and one public
1. Private static properties are used to hold object singletons. 2. Private constructors are used to prevent instantiation outside of a class. 3. Private __Clone prevents objects from being cloned outside the class. Public static methods are used to get singletons of objects.
Code:
// Three private and one public
class DB {
// Private static properties are used to hold singletons
private static $instance;
// Private constructors prevent instantiation outside the class
private function __construct() {}// The private __clone() prevents cloning objects outside of the class
private function __clone() {}// The public method is used to get singletons
public static function getInstance () {
if (!self: :$instance instanceof self) {
self: :$instance = new self(a); }return self: :$instance; }}$db1 = DB::getInstance();
// $db2 = clone $db1;
$db2 = DB::getInstance();
var_dump($db1.$db2)
? >
Copy the code
Effect:
3.2 Factory Mode
Features: Pass different parameters to invoke different policies. (method)
class ProductsA {}class ProductsB {}class Factory {
public function create($num) {
switch ($num) {
case 1:
return new ProductsA;
case 2:
return new ProductsB;
default:
return null; }}}/ / test
$factory = new Factory();
$obj1 = $factory->create(1);
$obj2 = $factory->create(2);
var_dump($obj1.$obj2);
? >
Copy the code
Effect:
3.3 Policy Mode
Features: Pass different parameters to call different policies (methods)
class Walk {
public function way() {
echo 'Walk to
'; }}class Bus {
public function way() {
echo 'Go by car
'; }}// Policy mode
class Student {
public function play ($obj) {
$obj->way(); }}/ / test
$stu = new Student;
// $stu->play(new Walk()); / / to walk
$stu->play(new Bus()); / / by car
? >
Copy the code
On the way to learning PHP, if you find this article helpful to you, then please pay attention to like comment 3 times, thank you, your must be another support of my blog.