如何使变量在类范围内可访问(面向对象编程PHP)
我有一个名为如何使变量在类范围内可访问(面向对象编程PHP),php,oop,object,Php,Oop,Object,我有一个名为testclass的类和几个函数。他们都需要访问一个预定义变量$need\u to\u have。我想在类中定义它,但是如何定义呢 例如: class testclass { // This won't work: private static $need_to_have = get_some_info(); public static testfunction1() { // Do Something with $need_to_have } pu
testclass
的类和几个函数。他们都需要访问一个预定义变量$need\u to\u have
。我想在类中定义它,但是如何定义呢
例如:
class testclass {
// This won't work:
private static $need_to_have = get_some_info();
public static testfunction1() {
// Do Something with $need_to_have
}
public testfunction2 () {
// Do something else with $need_to_have
}
}
忘了提到:我想让它私有,我只在静态函数上调用变量,所以我不能使用构造函数。将它作为类的属性,设置一次,然后在任何地方使用它:
class testclass
{
public static $need_to_have;
public function __construct()
{
//$this->need_to_have=1;
// Or rather the value returned by
// whatever it is you are doing with it
$this->need_to_have=$this->setNeed();
}
public function testfunction1()
{
// Do Something with $need_to_have
echo $this->need_to_have
}
public function testfunction2 ()
{
echo $this->need_to_have
}
public function setNeed()
{
$x=4;
$y=6;
$ret=$x*$y;
return $ret;
}
}
将其作为类的属性,设置一次并在任何地方使用:
class testclass
{
public static $need_to_have;
public function __construct()
{
//$this->need_to_have=1;
// Or rather the value returned by
// whatever it is you are doing with it
$this->need_to_have=$this->setNeed();
}
public function testfunction1()
{
// Do Something with $need_to_have
echo $this->need_to_have
}
public function testfunction2 ()
{
echo $this->need_to_have
}
public function setNeed()
{
$x=4;
$y=6;
$ret=$x*$y;
return $ret;
}
}
将其作为类的属性,设置一次并在任何地方使用:
class testclass
{
public static $need_to_have;
public function __construct()
{
//$this->need_to_have=1;
// Or rather the value returned by
// whatever it is you are doing with it
$this->need_to_have=$this->setNeed();
}
public function testfunction1()
{
// Do Something with $need_to_have
echo $this->need_to_have
}
public function testfunction2 ()
{
echo $this->need_to_have
}
public function setNeed()
{
$x=4;
$y=6;
$ret=$x*$y;
return $ret;
}
}
将其作为类的属性,设置一次并在任何地方使用:
class testclass
{
public static $need_to_have;
public function __construct()
{
//$this->need_to_have=1;
// Or rather the value returned by
// whatever it is you are doing with it
$this->need_to_have=$this->setNeed();
}
public function testfunction1()
{
// Do Something with $need_to_have
echo $this->need_to_have
}
public function testfunction2 ()
{
echo $this->need_to_have
}
public function setNeed()
{
$x=4;
$y=6;
$ret=$x*$y;
return $ret;
}
}
这几乎有很多方法可以做到 这里最流行的方法是使用所谓的getter(accessor)方法,该方法将返回有关属性的当前信息,并且仍然可以是私有的
class TestClass {
private static $myVar = 5;
public static getMyVar() {
return self::$myVar;
}
}
因此,使用适当的自动加载器,您可以在所有应用程序中调用
TestClass::getMyVar();
而且,正如你在评论中所说的,任何人都无权在课堂外更改它
然而,这可能被认为是一种不好的做法,不仅是因为静态方式,还因为您不应该引用一个与它没有任何耦合的类,而只是为了检索信息
这里最好的方法是实现注册表模式,在这里您可以将信息注册到全局空间
创建一个实现注册表设计模式的独立类,并可能设置一些约束,因此一旦设置了myVar
就没有人可以重置它,以便能够使用以下语法
Registry::get('myVar');
当然,这将使您有机会在将该属性注册到globalspace之前在该属性中添加更多的逻辑,因为定义属性可能会使您在某些定义方面遇到麻烦。这几乎有很多方法可以做到 这里最流行的方法是使用所谓的getter(accessor)方法,该方法将返回有关属性的当前信息,并且仍然可以是私有的
class TestClass {
private static $myVar = 5;
public static getMyVar() {
return self::$myVar;
}
}
因此,使用适当的自动加载器,您可以在所有应用程序中调用
TestClass::getMyVar();
而且,正如你在评论中所说的,任何人都无权在课堂外更改它
然而,这可能被认为是一种不好的做法,不仅是因为静态方式,还因为您不应该引用一个与它没有任何耦合的类,而只是为了检索信息
这里最好的方法是实现注册表模式,在这里您可以将信息注册到全局空间
创建一个实现注册表设计模式的独立类,并可能设置一些约束,因此一旦设置了myVar
就没有人可以重置它,以便能够使用以下语法
Registry::get('myVar');
当然,这将使您有机会在将该属性注册到globalspace之前在该属性中添加更多的逻辑,因为定义属性可能会使您在某些定义方面遇到麻烦。这几乎有很多方法可以做到 这里最流行的方法是使用所谓的getter(accessor)方法,该方法将返回有关属性的当前信息,并且仍然可以是私有的
class TestClass {
private static $myVar = 5;
public static getMyVar() {
return self::$myVar;
}
}
因此,使用适当的自动加载器,您可以在所有应用程序中调用
TestClass::getMyVar();
而且,正如你在评论中所说的,任何人都无权在课堂外更改它
然而,这可能被认为是一种不好的做法,不仅是因为静态方式,还因为您不应该引用一个与它没有任何耦合的类,而只是为了检索信息
这里最好的方法是实现注册表模式,在这里您可以将信息注册到全局空间
创建一个实现注册表设计模式的独立类,并可能设置一些约束,因此一旦设置了myVar
就没有人可以重置它,以便能够使用以下语法
Registry::get('myVar');
当然,这将使您有机会在将该属性注册到globalspace之前在该属性中添加更多的逻辑,因为定义属性可能会使您在某些定义方面遇到麻烦。这几乎有很多方法可以做到 这里最流行的方法是使用所谓的getter(accessor)方法,该方法将返回有关属性的当前信息,并且仍然可以是私有的
class TestClass {
private static $myVar = 5;
public static getMyVar() {
return self::$myVar;
}
}
因此,使用适当的自动加载器,您可以在所有应用程序中调用
TestClass::getMyVar();
而且,正如你在评论中所说的,任何人都无权在课堂外更改它
然而,这可能被认为是一种不好的做法,不仅是因为静态方式,还因为您不应该引用一个与它没有任何耦合的类,而只是为了检索信息
这里最好的方法是实现注册表模式,在这里您可以将信息注册到全局空间
创建一个实现注册表设计模式的独立类,并可能设置一些约束,因此一旦设置了myVar
就没有人可以重置它,以便能够使用以下语法
Registry::get('myVar');
当然,这将使您有机会在将该属性注册到globalspace之前在该属性中添加更多逻辑,因为定义属性可能会使您在某些定义方面遇到麻烦。您不能这样做,因为您无法使用非常量表达式初始化类属性 您可以做的是在您和属性之间放置一个访问器方法,例如:
class testclass {
private static $need_to_have;
private static $need_to_have_initialized;
public static testfunction1()
{
// Do Something with getNeedToHave()
}
private static function getNeedToHave()
{
if (!self::$need_to_have_initialized) {
self::$need_to_have = get_some_info();
self::$need_to_have_initialized = true;
}
return self::$need_to_have;
}
}
如果有一个常量值get_some_info
保证永远不会返回,则可以使用该常量值初始化$need_to_have
;这将允许您删除helper属性$need\u以初始化:
// example: get_some_info() never returns false
private static $need_to_have = false;
private static function getNeedToHave()
{
if (self::$need_to_have === false) {
self::$need_to_have = get_some_info();
}
return self::$need_to_have;
}
另一个可能的修改(改进?)是在getNe中使属性(以及“initialized”标志,如果适用)成为局部静态变量