Home Backend Development PHP Tutorial PHP 实例化类的一点摘记_php实例

PHP 实例化类的一点摘记_php实例

Jun 07, 2016 pm 05:26 PM
php kind


下面是调用一个模型(Module)的函数。这个函数的基本功能是指定一个模型(抽象化为类)的名称,然后它会在模型目录下面寻找这个类的脚本实例化以后返回。这样的做法有一点好处就是载入和实例化是自动的,你可以获得最大的灵活性。下面请看下面的代码,它并不长而且不复杂:

function &load_class($class_name, $param = null, $instantiate = true)
{
    static $objects = array();

    $class_name = ucfirst(strtolower($class_name));
    if (isset($objects[$class_name])) {
        return $objects[$class_name];
    }

    $class_file = DIR_MODELS . "/{$class_name}.inc.php";
    if (file_exists($class_file)) {
        require_once $class_file;

        if (!class_exists($class_name)) {
            return false;
        } else {
            $objects[$class_name] =& new $class_name($param);
            return $objects[$class_name];
        }

    }  else {
        if ($instantiate) {
            $objects[$class_name] = null;
        } 
        return null;
    }
}函数只有三个参数,分别是 $class_name 、$param 以及 $instaniate ,其中 $param 是构造函数的参数,$instaniate 是可选的。请注意函数中的 $objects 数组是一个静态变量,也就是当调用完这个函数的时候数组并不会释放,下次调用此函数时这个数组的数据是会保存的。这样做的好处就是可以将大部分的类实例了以后,如需要重复调用则直接返回这个类的实例就可以了,避免了重复调用,提高了性能。代码如下:

    static $objects = array();

    if (isset($objects[$class_name])) {
        return $objects[$class_name];
    }其它继续的代码就是检测是否有这个类名称的文件,如果有载入这个文件并寻找指定名称的类,如找到了这个类以后就实例化。这要求脚本中类的名称必须和脚本的文件名是一致的。我想这也有利于以后的代码管理。

$instaniate 参数这个时候就发挥了功效,这个参数会告诉函数如果未找到则在 $objects 下面做一个标记位(null)避免函数又重复的寻找文件名并重复载入和寻找。

    $class_file = DIR_MODELS . "/{$class_name}.inc.php";
    if (file_exists($class_file)) {
        require_once $class_file;

        if (!class_exists($class_name)) {
            return false;
        } else {
            $objects[$class_name] =& new $class_name($param);
            return $objects[$class_name];
        }

    }  else {
        if ($instantiate) {
            $objects[$class_name] = null;
        } 
        return null;
    }其中语句:

$objects[$class_name] =& new $class_name($param);可以多次的推敲一下。$class_name 在函数中是一个字符串变量。关键字 new 可以动态的实例化指定字符串的类(如果存在的话)。有关此调用方法可以参见 PHP 手册和这里。

此函数的不足之处就是如何去考虑传递不同个数的参数给每个不同的类的构造函数。或许可以使用 call_user_func_array 等函数实现,但是这样的做法非常的不 Grace。在这里需要推敲一下。其实 file_exists 等文件存在的测试可以交给 __autoload 函数处理,不过由于其他的函数比如 interface_exists 等也会调用 __autolaod 函数,出于兼容性的考虑,所以只在函数内做一个简单的测试。

PHP5 相对 PHP4 而言更加的面向对象。我想是时候更新我们我们的编码思想了。有关 PHP5 的类和对象,这里有一个非常好的教程。
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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
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)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

See all articles