Home Backend Development PHP Tutorial php Static关键字实用方法_PHP

php Static关键字实用方法_PHP

Jun 01, 2016 pm 12:19 PM
static Keywords

为了兼容PHP4,如果没有指定“可见性”,属性和方法默认为public。
由于静态方法不需要通过对象即可调用,所以伪变量$this在静态方法中不可用。
静态属性也可以由对象通过->操作符来访问。
用::方式调用一个非静态方法会导致一个E_STRICT级别的错误。
就像其它所有的PHP静态变量一样,静态属性只能被初始化为一个字符值或一个常量,不能使用表达式。 所以你可以把静态属性初始化为整型或数组,但不能指向另一个变量或函数返回值,也不能指向一个对象。
PHP5.3.0之后,我们可以用一个变量来动态调用类。但该变量的值不能为关键字self, parent 或static。
复制代码 代码如下:
class Foo
{
public static $my_static = 'foo';
public function staticValue() {
return self::$my_static;
}
}
class Bar extends Foo
{
public function fooStatic() {
return parent::$my_static;
}
}
print Foo::$my_static . "\n";
$foo = new Foo();
print $foo->staticValue() . "\n";
print $foo->my_static . "\n"; // Undefined "Property" my_static
print $foo::$my_static . "\n";
$classname = 'Foo';
print $classname::$my_static . "\n"; // PHP 5.3.0之后可以动态调用
print Bar::$my_static . "\n";
$bar = new Bar();
print $bar->fooStatic() . "\n";
?>

PHP里边用Static关键字来定义静态属性和方法.

实例一:静态属性的引用方法
复制代码 代码如下:
/*
*author:ajax123
*qq:283400245
*/
class person{
static$name="ajax123";//static声明静态属性
static$age=25;//static声明静态属性
static$address="北京";//static声明静态属性
function song(){
echo "My name is : ".self::$name."
";//类内部:通过通过self 类访问静态属性
echo "I am ".self::$age."
";//类内部:通过通过self 类访问静态属性
echo "I live in ".self::$address."
";//类内部:通过self 类访问静态属性
}
}
echoperson::$name."
";//类外部:通过类名person访问静态属性
echoperson::$age."
";//类外部:通过类名person访问静态属性
echoperson::$address."
";//类外部:通过类名person访问静态属性
?>


实例二:静态方法的引用方法

复制代码 代码如下:
/*
*author:ajax123
*qq:283400245
*/
class person{
static$name="ajax123";//static声明静态属性
static$age=25;//static声明静态属性
static$address="北京";//static声明静态属性
staticfunction song(){ //声明静态方法song
echo "My name is : ".self::$name."
";//类内部:通过通过self 类访问静态属性
echo "I am ".self::$age."
";//类内部:通过通过self 类访问静态属性
echo "I live in ".self::$address."
";//类内部:通过self 类访问静态属性
}
}
person::song()."
";//类外部:通过类名person访问静态方法
?>

Statement of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

In-depth analysis of the role and usage of the static keyword in C language In-depth analysis of the role and usage of the static keyword in C language Feb 20, 2024 pm 04:30 PM

In-depth analysis of the role and usage of the static keyword in C language

The role and examples of var keyword in PHP The role and examples of var keyword in PHP Jun 28, 2023 pm 08:58 PM

The role and examples of var keyword in PHP

Is go a keyword in C language? Detailed analysis Is go a keyword in C language? Detailed analysis Mar 16, 2024 am 10:30 AM

Is go a keyword in C language? Detailed analysis

How many keywords are there in c language? How many keywords are there in c language? Nov 22, 2022 pm 03:39 PM

How many keywords are there in c language?

What is the function and usage of static in C language What is the function and usage of static in C language Jan 31, 2024 pm 01:59 PM

What is the function and usage of static in C language

How to use static, this, super, and final in Java How to use static, this, super, and final in Java Apr 18, 2023 pm 03:40 PM

How to use static, this, super, and final in Java

Complete list of go language keywords Complete list of go language keywords Apr 07, 2024 pm 02:15 PM

Complete list of go language keywords

Detailed explanation of the role and usage of the extends keyword in PHP Detailed explanation of the role and usage of the extends keyword in PHP Jun 28, 2023 pm 08:04 PM

Detailed explanation of the role and usage of the extends keyword in PHP

See all articles