search
HomeBackend DevelopmentPHP TutorialDetailed explanation of usage examples of static keyword in functions in PHP

static is a static variable in php. He can define functions. The variable is a global static variable. So what impact will we add static in front of the function or variable to the function and variable? Well, let’s take a look together.  

1) The description of global variables (external variables) is preceded by static to constitute a static global variable. Global variables themselves are static storage methods, and static global variables are of course also static storage methods. There is no difference between the two in the way they are stored. The difference between the two is that the scope of non-static global variables is the entire source program. When a source program consists of multiple source files, non-static global variables are valid in each source file. . Static global variables limit their scope, that is, they are only valid within the source file in which the variable is defined, and cannot be used in other source files of the same source program. Since the scope of static global variables is limited to one source file and can only be shared by functions in that source file, errors can be avoided in other source files.

2) From the above analysis, it can be seen that changing a local variable to a static variable changes its storage method and changes its lifetime. Changing a global variable to a static variable changes its scope and limits its scope of use.​

3) The static function is different from the ordinary

function scope , only in this file. Functions used only in the current source file should be described as internal functions(static), and internal functions should be described and defined in the current source file. For functions that can be used outside the current source file, they should be stated in a header file. The source files that want to use these functions must include this header file.

After PHP5.3.0, we can use a variable to dynamically call it kind. But the value of this variable cannot be the keywords self, parent or static.

Example #1 Static member code example

<?php
class Foo
{
    public static $my_static = &#39;foo&#39;;
    public function staticValue() {
       return self::$my_static;
    }
}
class Bar extends Foo
{
    public function fooStatic() {
        return parent::$my_static;
    }
}

print Foo::$my_static . " ";
$foo = new Foo();
print $foo->staticValue() . " ";
print $foo->my_static . " ";      // Undefined "Property" my_static
print $foo::$my_static . " ";
$classname = &#39;Foo&#39;;
print $classname::$my_static . " "; // PHP 5.3.0之后可以动态调用
print Bar::$my_static . " ";
$bar = new Bar();
print $bar->fooStatic() . " ";
?>

Example #2 Static method code example

<?php
class Foo {
    public static function aStaticMethod() {
        // ...
    }
}
Foo::aStaticMethod();
$classname = &#39;Foo&#39;;
$classname::aStaticMethod(); // As of PHP 5.3.0
?>

About the use of the

Static keyword in the class, The PHP manual gives the following conventions:

1. Declare class members or methods as static, and you can access them directly without instantiating the class. Static members (except static methods) cannot be accessed through an object.

2. Since static methods do not need to be called through objects, the pseudo variable $this is not available in static methods.

3. Static properties cannot be accessed by objects through the -> operator.
4. Calling a non-static method using the :: method will cause an E_STRICT level error.
Now let’s focus on Article 4.
Running environment: (Win32) PHP/5.3.3

class Foo{       
    public static $my_static = &#39;foo&#39;;//声明一个静态成员  
    public function staticValue() {//静态方法  
       return self::$my_static;//  
    }  
    public function run(){//非静态方法  
      return "abc <br>";  
    }  
    public  function callrun() {  
        return self::run();//用self::方式调用一个非静态方法  
             
    }  
      
}  
     
echo Foo::$my_static . "<br >";  
     
echo Foo::run();//用className::方法名调用非静态方法  
echo Foo::callrun();

static keyword function:

The scope of use of static variables in PHP is wider. We can not only use it in classes, Add the static modifier in front of the method or variable, and we can even add the static keyword to the variables inside the function. The value of a variable with the static modifier added will not be lost even after the function is executed. That is to say, the variable will still remember the original value the next time the function is called.




The above is the detailed content of Detailed explanation of usage examples of static keyword in functions in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么查找字符串是第几位php怎么查找字符串是第几位Apr 22, 2022 pm 06:48 PM

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。

php怎么设置implode没有分隔符php怎么设置implode没有分隔符Apr 18, 2022 pm 05:39 PM

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

See all articles

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 agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment