关于 php静态变量的问题
今天想学习一下 phpcms, 然后就看了一下源代码,但是在 看 _load_class 这个方法的时候有点疑问。
<code>/** * 加载类文件函数 * @param string $classname 类名 * @param string $path 扩展地址 * @param intger $initialize 是否初始化 */ private static function _load_class($classname, $path = '', $initialize = 1) { static $classes = array(); if (empty($path)) $path = 'libs'.DIRECTORY_SEPARATOR.'classes'; $key = md5($path.$classname); if (isset($classes[$key])) { if (!empty($classes[$key])) { return $classes[$key]; } else { return true; } } if (file_exists(PC_PATH.$path.DIRECTORY_SEPARATOR.$classname.'.class.php')) { include PC_PATH.$path.DIRECTORY_SEPARATOR.$classname.'.class.php'; $name = $classname; if ($my_path = self::my_path(PC_PATH.$path.DIRECTORY_SEPARATOR.$classname.'.class.php')) { include $my_path; $name = 'MY_'.$classname; } if ($initialize) { $classes[$key] = new $name; } else { $classes[$key] = true; } return $classes[$key]; } else { return false; } }</code>
static $classes = array(); 如果没有加上static 属性的话就是申明了一个空数组,是不是加上 static之后,第多次调用之后这个数组的值其实是上一次改变之后的值?我没明白他是如何将值存入这个$classes 数组里面的,求解。或者有熟悉的朋友讲解一下phpcms怎么实现调用类和函数的。不胜感激!
回复内容:
今天想学习一下 phpcms, 然后就看了一下源代码,但是在 看 _load_class 这个方法的时候有点疑问。
<code>/** * 加载类文件函数 * @param string $classname 类名 * @param string $path 扩展地址 * @param intger $initialize 是否初始化 */ private static function _load_class($classname, $path = '', $initialize = 1) { static $classes = array(); if (empty($path)) $path = 'libs'.DIRECTORY_SEPARATOR.'classes'; $key = md5($path.$classname); if (isset($classes[$key])) { if (!empty($classes[$key])) { return $classes[$key]; } else { return true; } } if (file_exists(PC_PATH.$path.DIRECTORY_SEPARATOR.$classname.'.class.php')) { include PC_PATH.$path.DIRECTORY_SEPARATOR.$classname.'.class.php'; $name = $classname; if ($my_path = self::my_path(PC_PATH.$path.DIRECTORY_SEPARATOR.$classname.'.class.php')) { include $my_path; $name = 'MY_'.$classname; } if ($initialize) { $classes[$key] = new $name; } else { $classes[$key] = true; } return $classes[$key]; } else { return false; } }</code>
static $classes = array(); 如果没有加上static 属性的话就是申明了一个空数组,是不是加上 static之后,第多次调用之后这个数组的值其实是上一次改变之后的值?我没明白他是如何将值存入这个$classes 数组里面的,求解。或者有熟悉的朋友讲解一下phpcms怎么实现调用类和函数的。不胜感激!
第一次调用,
static $classes = array()
初始化$classes
静态变量后,在内存中就有一个地址(假设为0x123),只要php脚本没有终止,下次再调用static $classes = array()
并不会再次初始化$classes
;之后对$classes
所有操作都是对这个地址(0x123)操作;

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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

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

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

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

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

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

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

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