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











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.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.
