PHP中的静态变量及static静态变量使用详解,static使用详解
PHP中的静态变量及static静态变量使用详解,static使用详解
静态变量只存在于函数作用域内,也就是说,静态变量只存活在栈中。一般的函数内变量在函数结束后会释放,比如局部变量,但是静态变量却不会。就是说,下次再调用这个函数的时候,该变量的值会保留下来。
只要在变量前加上关键字static,该变量就成为静态变量了。
<?php function test() { static $nm = ; $nm = $nm * ; print $nm."<br />"; } // 第一次执行,$nm = test(); // 第一次执行,$nm = test(); // 第一次执行,$nm = test(); ?>
程序运行结果:
1
2
2
4
3
8
函数test()执行后,变量$nm的值都保存了下来了。
在class中经常使用到静态属性,比如静态成员、静态方法。
Program List:类的静态成员
静态变量$nm属于类nowamagic,而不属于类的某个实例。这个变量对所有实例都有效。
::是作用域限定操作符,这里用的是self作用域,而不是$this作用域,$this作用域只表示类的当前实例,self::表示的是类本身。
<?php class nowamagic { public static $nm = ; function nmMethod() { self::$nm += ; echo self::$nm . '<br />'; } } $nmInstance = new nowamagic(); $nmInstance -> nmMethod(); $nmInstance = new nowamagic(); $nmInstance -> nmMethod(); ?>
程序运行结果:
1
3
2
5
Program List:静态属性
<?php class NowaMagic { public static $nm = 'www.nowamagic.net'; public function nmMethod() { return self::$nm; } } class Article extends NowaMagic { public function articleMethod() { return parent::$nm; } } // 通过作用于限定操作符访问静态变量 print NowaMagic::$nm . "<br />"; // 调用类的方法 $nowamagic = new NowaMagic(); print $nowamagic->nmMethod() . "<br />"; print Article::$nm . "<br />"; $nmArticle = new Article(); print $nmArticle->nmMethod() . "<br />"; ?>
程序运行结果:
www.nowamagic.net
www.nowamagic.net
www.nowamagic.net
www.nowamagic.net
Program List:简单的静态构造器
PHP没有静态构造器,你可能需要初始化静态类,有一个很简单的方法,在类定义后面直接调用类的Demonstration()方法。
<?php function Demonstration() { return 'This is the result of demonstration()'; } class MyStaticClass { //public static $MyStaticVar = Demonstration(); //!!! FAILS: syntax error public static $MyStaticVar = null; public static function MyStaticInit() { //this is the static constructor //because in a function, everything is allowed, including initializing using other functions self::$MyStaticVar = Demonstration(); } } MyStaticClass::MyStaticInit(); //Call the static constructor echo MyStaticClass::$MyStaticVar; //This is the result of demonstration() ?>
程序运行结果:
This is the result of demonstration()
下面给大家介绍PHP静态变量static的使用介绍
static关键字在C#编程中非常常见,它用来修饰符声明属于类型本身而不是属于特定对象的静态成员。static 修饰符可用于类、字段、方法、属性、运算符、事件和构造函数,但不能用于索引器、析构函数或类以外的类型。声明为static的类、函数和变量将不能引用实例方法或变量,另外在C#中一旦类被添加了static修饰符,则其内部所有变量和方法都必须是静态的。静态变量和方法必须通过类名进行引用而不能通过实例对象引用。
那么static关键字在php中与C#中都有些什么不同呢?
声明范围
相对C#来说,PHP中static变量的使用范围要更广一些,我们不仅可以在类,方法或变量前面添加static修饰符,我们甚至还能给函数内部变量添加static关键字。添加了static修饰符的变量即使在该函数执行完毕值仍然不会丢失,也就是说,在下一次调用这个函数时,变量仍然记得原来的值。如:
<?php function test() { static $var=; $var+=; echo $var.' '; } test(); test(); test(); ?>
运行结果如下:
3 5 7
这里有一点需要注意的是,变量的赋值操作只会在变量第一次初始化时会被调用,在之后函数的执行过程中,这个操作不会被调用。
由于PHP中函数同样是一等公民,所以不同于C#,我们可以直接定义函数,并直接在代码的任何地方调用,这一点跟javascript倒是有些相似。因此这时候函数静态变量会比定义全局变量更加有用,它可以避免变量重复定义导致冲突。而由于C#中函数无法直接定义和调用,它必须寄宿在类中,所以如果函数需要静态变量,我们只需要在类中定义便能达到相同效果。
调用方式
C#中,我们调用静态成员的方式非常简单和一致,因为静态成员不属于实例对象,所以无论是方法还是变量,C#对其静态成员的访问方式一律是通过类名.方法(变量)进行。并且在C#中,静态函数是不能被设置为虚方法或者覆盖的。而PHP对此则提供了更为灵活多样的支持。
首先,我们知道PHP中调用实例方法都是通过如:someobj->someFun()调用,那么我们调用静态函数是否也能像C#那样通过SomeClass->someFun()调用呢?答案是否定的,在PHP中,对静态成员的调用只能通过::的方式进行,如:SomeClass::someFun()。
<?php class TestC { public static $var=; public $var=; function t() { self::$var+=; echo self::$var.' '; echo $this->var.' '; } public static function t() { self::$var+=; echo self::$var.' '; } } $t=new TestC(); $t->t(); TestC::t(); ?>
运行结果如下:
3 1 5
另外一点和C#中不同的是,在类中的方法中,如果我们需要调用静态变量,必须通过self::$somVar静态变量(注意变量前面的$符号,实例变量不需要),而调用静态方法则为self::someFun()(这里不需要$符号)。如上例。
另外,与C#最大的不同之处就是,PHP中,子类是可以覆盖父类的静态函数或变量的,不仅如此,(站在C#程序员的角度来看,我觉得PHP这点反而将事情搞复杂了),由于默认self::staticFun()调用的是子类的静态函数,这个时候如果我们想调用父类的静态变量怎么办呢?这里PHP提供了额外的parent来调用基类的静态成员。如:
<?php class TestC { public static $var=; public $var=; function t() { self::$var+=; echo self::$var.' '; echo $this->var.' '; } public static function t() { self::$var+=; echo self::$var.' '; } } $t=new TestC(); $t->t(); TestC::t(); ?>
运行结果如下:
3 5 ‘Hello'
最好,根据上面的例子我们很容易想到,子类访问父类可以使用parent关键字,那么父类如何访问子类的静态方法呢?这里给出static的另外一个用法,这里如果将调用的静态方法前面的作用域换成static的话,PHP会根据该类的继承层次来计算最终的静态方法。如:
<?php class Test { function t() { static::t(); } public static function t() { echo self::'Test '; } } class Test extends Test { static function t() { echo self::'Test '; } } $t=new Test(); $t->t(); Test::t(); ?>
运行结果如下:
Test2 Test2
这里t实例在t1方法调用t2静态方法时,会根据其实例找到最终的静态方法并输出Test2。
总结
从上面的分析,我们不难看出,对于静态成员的使用,PHP提供了比C#更为强大的功能或灵活性,但从我的角度来看,这种灵活性不见得就更好,从某种角度来看,如果类的继承层次过于复杂,它可能会让我产生混淆。当然,同样的工具不同人使用效果会完全不一样,既然PHP提供了更多样的选择,那么相信如果使用恰当的话,PHP中的static可能会提供比C#中更为强大而简便的使用方式。

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











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 is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

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.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.
