php5.5 const,关于php:PHP5:const vs static

在php5中,使用const和static有什么区别?什么时候合适?如果有的话,public、protected和private扮演什么角色?

在类的上下文中,静态变量在类范围(而不是对象)范围内,但与常量不同,它们的值可以更改。

class ClassName {

static $my_var = 10;  /* defaults to public unless otherwise specified */

const MY_CONST = 5;

}

echo ClassName::$my_var;   // returns 10

echo ClassName::MY_CONST;  // returns 5

ClassName::$my_var = 20;   // now equals 20

ClassName::MY_CONST = 20;  // error! won't work.

public、protected和private在consts方面不相关(consts总是public);它们只对类变量(包括静态变量)有用。

公共静态变量可以通过classname::$variable在任何地方访问。

受保护的静态变量可以通过定义类或通过classname::$variable扩展类来访问。

只有通过classname::$variable定义类才能访问私有静态变量。

编辑:需要注意的是,php 7.1.0引入了对指定类常量可见性的支持。

我更喜欢将self::$variable用于受保护的静态变量和私有静态变量,因为我更喜欢将所提到的类名只保留一次,即在类的最开始。

是的,很好的一点,我忽略了提到self关键字可以在类内部引用时使用。我上面提供的示例是在类定义之外执行的,在这种情况下,必须使用类名。

回答很好,非常接近接受。你能澄清一点吗:"公共的、受保护的和私有的在常量方面是不相关的"—为什么?默认情况下的常量都是公共的吗?都是私人的?

静态变量是否不需要$?定义中的static $my_var = 10;。

旧线程,但我想添加一些东西:查看php.net/manual/en/…,这解释了static变量在单例函数和递归函数中也非常有用。因为您可以更改该值,但变量只能初始化一次。请参阅stackoverflow.com/questions/203336/…,了解如何创建单例。对我来说,这些情况下我更喜欢静态变量。

最后一点应该指出的是常量总是静态的和公共的。这意味着您可以从类内访问const,如下所示:

class MyClass

{

const MYCONST = true;

public function test()

{

echo self::MYCONST;

}

}

从类外部,您可以这样访问它:

echo MyClass::MYCONST;

那声明是真的吗?"常量总是静态的和公共的"?

这不再是真的了。从php 7.1开始,类常量可以声明为private或protected。见RFC

常量只是一个常量,即不能在声明后更改其值。

静态变量可以在不生成类实例的情况下访问,因此在类的所有实例之间共享。

此外,函数中可以有一个静态局部变量,该变量只能声明一次(在函数的第一次执行时),并且可以在函数调用之间存储其值,例如:

function foo()

{

static $numOfCalls = 0;

$numOfCalls++;

print("this function has been executed" . $numOfCalls ." times");

}

在讨论类继承时,可以使用self和static关键字来区分不同范围中的常量或变量。检查此示例,其中说明了如何访问:

class Person

{

static $type = 'person';

const TYPE = 'person';

static public function getType(){

var_dump(self::TYPE);

var_dump(static::TYPE);

var_dump(self::$type);

var_dump(static::$type);

}

}

class Pirate extends Person

{

static $type = 'pirate';

const TYPE = 'pirate';

}

然后这样做:

$pirate = new Pirate();

$pirate::getType();

或:

Pirate::getType();

输出:

string(6)"person"

string(6)"pirate"

string(6)"person"

string(6)"pirate"

将类方法或属性声明为static使它们可以访问,而无需实例化类。

类常量和普通常量一样,不能在运行时更改。这也是使用const的唯一原因。

private、public和protected是访问修饰符,用于描述谁可以访问哪个参数/方法。

public意味着所有其他对象都可以访问。private意味着只有实例化的类才能获得访问权。保护意味着实例化类和派生类可以访问。

以下是我迄今为止学到的关于静态成员、常量变量和访问修饰符(private、public和protected)的知识。常数

定义

就像名字所说的,常量变量的值是不可更改的。常量不同于普通变量,因为您不需要使用$符号来声明或使用它们。

该值必须是常量表达式,而不是(例如)变量、属性、数学运算的结果或函数调用。

Note : The variable's value can not be a keyword (e.g. self, parent

and static).

在PHP中声明常量

class constantExample{

const CONSTANT = 'constant value'; //constant

}

?>

常量的作用域是全局的,可以使用self关键字访问。

class MyClass

{

const CONSTANT = 'constant value';

function showConstant() {

echo  self::CONSTANT ."

";

}

}

echo MyClass::CONSTANT ."

";

$classname ="MyClass";

echo $classname::CONSTANT ."

"; // As of PHP 5.3.0

$class = new MyClass();

$class->showConstant();

echo $class::CONSTANT."

"; // As of PHP 5.3.0

?>

静态的

定义

静态关键字可用于声明类、成员函数或变量。类中的静态成员是全局成员,也可以使用self关键字访问。将类属性或方法声明为静态将使它们可以访问,而无需实例化类。不能使用实例化的类对象(尽管静态方法可以)访问声明为静态的属性。如果不使用可见性声明(public、private、protected),则该属性或方法将被视为已声明为public。因为静态方法在没有创建对象实例的情况下是可调用的。

Note : the pseudo-variable $this is not available inside the method

declared as static.Static properties cannot be accessed through the

object using the arrow operator ->

As of PHP 5.3.0, it's possible to reference the class using a variable. The >variable's value cannot be a keyword (e.g. self, parent and static).

静态属性示例

class Foo

{

public static $my_static = 'foo'; //static variable

public static function staticValue() { //static function example

return self::$my_static;  //return the static variable declared globally

}

}

?>

访问静态属性和函数示例

print Foo::$my_static ."

";

$foo = new Foo();

print $foo->staticValue() ."

";

print $foo->my_static ."

";      // Undefined"Property" my_static

print $foo::$my_static ."

";

$classname = 'Foo';

print $classname::$my_static ."

"; // As of PHP 5.3.0

print Bar::$my_static ."

";

$bar = new Bar();

print $bar->fooStatic() ."

";

?>

公共、私有、受保护(即访问修饰符)

在阅读下面的定义之前,请阅读这篇关于封装的文章,它将帮助您更深入地理解这个概念。

链接1维基百科

关于封装的教程点链接

定义

使用私有的、公共的、受保护的关键字,可以控制对类中成员的访问。声明为公共的类成员可以在任何地方访问。声明为受保护的成员只能在类本身内以及由继承类和父类访问。声明为私有的成员只能由定义该成员的类访问。

例子

class Example{

public $variable = 'value'; // variable declared as public

protected $variable = 'value' //variable declared as protected

private $variable = 'value'  //variable declared as private

public function functionName() {  //public function

//statements

}

protected function functionName() {  //protected function

//statements

}

private function functionName() {  //private function

//statements

}

}

?>

访问公共、私有和受保护的成员示例

Public variable's can be accessed and modified from outside the class

or inside the class. But You can access the private and protected variables and functions only from inside the class , You can't modify the value of protected or Public members outside the class.

class Example{

public $pbVariable = 'value';

protected $protVariable = 'value';

private $privVariable = 'value';

public function publicFun(){

echo $this->$pbVariable;  //public variable

echo $this->$protVariable;  //protected variable

echo $this->privVariable; //private variable

}

private function PrivateFun(){

//some statements

}

protected function ProtectedFun(){

//some statements

}

}

$inst = new Example();

$inst->pbVariable = 'AnotherVariable'; //public variable modifed from outside

echo $inst->pbVariable;   //print the value of the public variable

$inst->protVariable = 'var'; //you can't do this with protected variable

echo $inst->privVariable; // This statement won't work , because variable is limited to private

$inst->publicFun(); // this will print the values inside the function, Because the function is declared as a public function

$inst->PrivateFun();   //this one won't work (private)

$inst->ProtectedFun();  //this one won't work as well (protected)

?>

有关更多信息,请阅读此PHP文档关于可见性和可见性的PHP文档

参考文献:php.net

希望你理解这个概念。谢谢你的阅读:)祝你过得愉快

回顾一下@matt great answer:

在大多数情况下,您需要一个私有/受保护的静态属性,因此常量不是一个选项。

如果您需要的属性是公开可用的,但没有更改,则A常量是正确的选择。

例子:

class User{

private static $PASSWORD_SALT ="ASD!@~#asd1";

...

}

class Product{

const INTEREST = 0.10;

...

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值