我想要以下内容:
<?php
class Context
{
public static $ClientID = "";
public static $ClientSecret = "";
public static function RedirectURI()
{
return "http://" . $_SERVER[ "HTTP_HOST" ] ."/authenticate";
}
}
会变成这样:
<?php
class Context
{
public static $ClientID = "";
public static $ClientSecret = "";
public static $RedirectURI;
protected function getRedirectURI()
{
return "http://" . $_SERVER[ "HTTP_HOST" ] ."/authenticate";
}
}
这意味着当人们希望访问RedirectURI属性时,API将更加直观和易于理解:
echo Context::$ClientID;
echo Context::$RedirectURI;
有没有办法像在C#中一样在PHP中执行此操作:
public class Context
{
public static string ClientID { get; set; }
public static string ClientSecret = "";
public static string RedirectURI {
get
{
return "http://" . $_SERVER[ "HTTP_HOST" ] ."/authenticate";
};
set{}
}
}
我知道__Get(),但似乎你不能有一个明确定义的模型,因为不能设置属性才能对它采取行动?
<?php
class Context
{
public static $ClientID = "";
public static $ClientSecret = "";
public static function __Get( $field )
{
switch( $field )
{
case "RedirectURI":
return "http://" . $_SERVER[ "HTTP_HOST" ] ."/authenticate";
break;
default:
throw new Exception( "Property {field} not defined" );
}
}
}
为了让生活更轻松,$_SERVER[ "" ]
不能用于财产申报......因此问题。
我可以创建一个全局变量并从属性中引用它,但我不喜欢这种方法。