php中静态类与静态变量用法的区别分析_php技巧
本文实例分析了php中静态类与静态变量用法的区别。分享给大家供大家参考。具体分析如下:
static是定义一个静态对象或静态变量,关于static 定义的变量或类方法有什么特性我们看完本文章的相关实例后就见分晓了.
1. 创建对象$object = new Class(),然后使用”->”调用:$object->attribute/function,前提是该变量/方法可访问.
2. 直接调用类方法/变量:class::attribute/function,无论是静态/非静态都可以,但是有前提条件.
A. 如果是变量,需要该变量可访问.
B. 如果是方法,除了该方法可访问外,还需要满足.
① 如果是静态方法,没有特殊条件.
② 如果是非静态方法,需要改方法中没有使用$this,即没有调用非静态的变量/方法,当然,调用静态的变量/方法没有问题.
然后我们再看一下使用$object->… 和使用class::… 都有什么区别:
1. 使用$object->… ,需要执行构造函数创建对象.
2. 使用class::… 调用静态方法/变量,不需要执行构造函数创建对象.
3. 使用class::… 调用非静态方法/变量,也不需要执行构造函数创建对象.
然后奇怪的地方就出来了,既然2和3都一样,那静态方法/变量存在还有什么意义呢?
静态static:声明类成员或方法为 static,就可以不实例化类而直接访问,不能通过一个对象来访问其中的静态成员(静态方法除外),静态成员属于类,不属于任何对象实例,但类的对象实例都能共享.
例子,代码如下:
// 定义静态成员属性
public static $country = "中国";
// 定义静态成员方法
public static function myCountry() {
// 内部访问静态成员属性
echo "我是".self::$country."人
";
}
}
class Student extends Person {
function study() {
echo "我是". parent::$country."人
";
}
}
// 输出成员属性值
echo Person::$country."
"; // 输出:中国
$p1 = new Person();
//echo $p1->country; // 错误写法
// 访问静态成员方法
Person::myCountry(); // 输出:我是中国人
// 静态方法也可通过对象访问:
$p1->myCountry();
// 子类中输出成员属性值
echo Student::$country."
"; // 输出:中国
$t1 = new Student();
$t1->study(); // 输出:我是中国人
?>
运行该例子,输出:
中国
我是中国人
我是中国人
中国
我是中国人
小结:在类内部访问静态成员属性或者方法,使用 self::(注意不是 $slef),代码如下:
slef:: myCountry()
在子类访问父类静态成员属性或方法,使用 parent::(注意不是 $parent),代码如下:
parent:: myCountry()
外部访问静态成员属性和方法为 类名/子类名::,代码如下:
Person::myCountry()
Student::$country
但静态方法也可以通过普通对象的方式访问.
例子,声明静态变量,代码如下:
static $int = 0;// correct
static $int = 1+2; // wrong (as it is an expression)
static $int = sqrt(121); // wrong (as it is an expression too)
$int++;
echo $int;
}
?>
例子,使用静态变量的例子,代码如下:
{
static $w3sky = 0;
echo $w3sky;
$w3sky++;
}
?>
现在,每次调用 Test() 函数都会输出 $w3sky 的值并加一.
静态变量也提供了一种处理递归函数的方法,递归函数是一种调用自己的函数,写递归函数时要小心,因为可能会无穷递归下去,必须确保有充分的方法来中止递归,一下这个简单的函数递归计数到 10,使用静态变量 $count 来判断何时停止.
例子,静态变量与递归函数,代码如下:
{
static $count = 0;
$count++;
echo $count;
if ($count Test();
}
$count--;
}
?>
注:静态变量可以按照上面的例子声明,如果在声明中用表达式的结果对其赋值会导致解析错误.
希望本文所述对大家的php程序设计有所帮助。

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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 PHP, you can effectively prevent CSRF attacks by using unpredictable tokens. Specific methods include: 1. Generate and embed CSRF tokens in the form; 2. Verify the validity of the token when processing the request.

H5. The main difference between mini programs and APP is: technical architecture: H5 is based on web technology, and mini programs and APP are independent applications. Experience and functions: H5 is light and easy to use, with limited functions; mini programs are lightweight and have good interactiveness; APPs are powerful and have smooth experience. Compatibility: H5 is cross-platform compatible, applets and APPs are restricted by the platform. Development cost: H5 has low development cost, medium mini programs, and highest APP. Applicable scenarios: H5 is suitable for information display, applets are suitable for lightweight applications, and APPs are suitable for complex functions.

Export password-protected PDF in Photoshop: Open the image file. Click "File"> "Export"> "Export as PDF". Set the "Security" option and enter the same password twice. Click "Export" to generate a PDF file.

Strict types in PHP are enabled by adding declare(strict_types=1); at the top of the file. 1) It forces type checking of function parameters and return values to prevent implicit type conversion. 2) Using strict types can improve the reliability and predictability of the code, reduce bugs, and improve maintainability and readability.

In PHP, the final keyword is used to prevent classes from being inherited and methods being overwritten. 1) When marking the class as final, the class cannot be inherited. 2) When marking the method as final, the method cannot be rewritten by the subclass. Using final keywords ensures the stability and security of your code.

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.
