PHP类获取全局变量
我正在使用 我想通过PHP类获取全局变量,php,twitter,Php,Twitter,我正在使用 我想通过index.php?consumer\u key=xxx。 在通过这个之后,我想进入PHP类,但是我得到了一个错误 下面是代码。完整代码: 请提供帮助。您不能使用$variable设置属性类值,在这种情况下,您需要在构造类之前进行设置 class ezTweet { private $consumer_key = ''; private $consumer_secret = 'xxxx'; public function __construct(
index.php?consumer\u key=xxx
。
在通过这个之后,我想进入PHP类,但是我得到了一个错误
下面是代码。完整代码:
请提供帮助。您不能使用$variable设置属性类值,在这种情况下,您需要在构造类之前进行设置
class ezTweet
{
private $consumer_key = '';
private $consumer_secret = 'xxxx';
public function __construct($consumer_key)
{
if (!is_string($consumer_key) || !strlen(trim($consumer_key))) {
throw new \InvalidArgumentException('"consumer_key" cannot be empty!');
}
$this->consumer_key = $consumer_key;
}
public function getConsumerKey()
{
return $this->consumer_key;
}
}
$consumer_key = (isset($_GET['consumer_key']) ? $_GET['consumer_key'] : null);
try {
$ezTweet = new ezTweet($consumer_key);
// [...]
}
catch (\InvalidArgumentException $InvalidArgumentException) {
echo $InvalidArgumentException->getMessage();
}
类是一个独立、可重用和可移植的代码单元。
因此,在任何情况下,它都不应该依赖全局变量来初始化其属性或执行其工作 如果需要类访问某个值,请通过构造函数或setter方法将该值传递给该类:
class EzTweet
{//class names start with UpperCase, and the { goes on the next line
private $consumer_key = null;//initialize to default, null
public function __construct($cKey = null)
{//pass value here, but default to null -> optional
$this->consumer_key = $cKey;//set value
}
public function setConsumerKey($cKey)
{
$this->consumer_key = $cKey;//set value later on through setter
return $this;
}
}
//use:
$ezTwitter = new EzTwitter();
if (isset($_GET['consumer_key']))
$ezTwitter->SetConsumerKey($_GET['consumer_key']);//set value
不管怎样,这就是我要做的。顺便说一句:请检查,并尽量坚持,这一点
更新:
事实证明你已经有了一个构造函数。好的,只需将其更改为:
public function __construct($cKey = null)//add argument
{
$this->consumer_key = $cKey;//add this
// Initialize paths and etc.
$this->pathify($this->cache_dir);
$this->pathify($this->lib);
$this->message = '';
// Set server-side debug params
if ($this->debug === true)
error_reporting(-1);
else
error_reporting(0);
}
为什么两个变量的名称相同?类是一个独立的、可重用的代码单元。因此,在任何情况下,它都不应该依赖全局变量来初始化其属性或执行其工作。如果您需要一个类来访问某个值,请将该值传递给该类。我只是将它们添加到此处进行测试和解释。是否有人可以修复我的代码并初始化所有变量:@MoinAKbar:此站点不是免费的调试器服务,但我可能愿意重构/修复该问题,但这一切都取决于你愿意支付多少…@MoinAkbar:重构和调试是你的工作。如果您想编写代码,您必须接受并习惯调试是工作的一部分这一事实。但是,查看您在另一条评论中发布的错误消息:只需将
$cKey=null
添加到现有构造函数参数列表(空atm),然后添加$this->consumer\u key=$cKey代码>方法主体的行。不要再添加第二个CONSTRUCTOR@MoinAKbar:编辑了我的答案,如果此答案帮助了您,@Elias Van Ootegem“setConsumerKey”是一个漏洞,任何具有该实例的人都可以更改“consumer_key”是,但您可以在setter中执行一些数据验证。此外,任何人都可以以任何方式创建此CLSA的实例,并将任何值传递给构造函数。什么能阻止我只在你的代码片段中添加一行代码>?同样,只有当我有2个有效密钥时,我才能用你的代码创建2个实例,setter允许我重复使用单个实例,并且(再次)对站点中的值执行一些验证(例如:setEmail
callsfilter\u var($argument,filter\u VALIDATE\u EMAIL)
),private
并不意味着它是无懈可击的:尝试var_dump((数组)$objWithPrivate)
或者如果我在序列化($instance)
上做了一些str\u replace
/preg\u replace
欺骗,然后尝试$instance=unserialize($alteredString)代码>?而这些只是最难的部分。那么神奇的接球手和二传手呢?我100%反对这些神奇的方法,但它们经常被实现,有效地公开了所有私有属性。。。setter并不是一个很大的安全风险
public function __construct($cKey = null)//add argument
{
$this->consumer_key = $cKey;//add this
// Initialize paths and etc.
$this->pathify($this->cache_dir);
$this->pathify($this->lib);
$this->message = '';
// Set server-side debug params
if ($this->debug === true)
error_reporting(-1);
else
error_reporting(0);
}