简体   繁体   English

PHP-致命错误:不在对象上下文中时使用$ this

[英]PHP - Fatal error: Using $this when not in object context

I'm really sorry to be asking this question because I know it has been asked many times before (and I have read the threads) but I just can't puzzle this out. 我真的很抱歉提出这个问题,因为我知道之前已经问过很多次了(而且我已经阅读了线程),但是我对此不知所措。

I have a class: 我有一堂课:

class Fivehundredpx {
    private $_user;
    public $fivehundredpx;

    public function __construct(User $user){ 
        $this->_user = $user->data();
        $this->fivehundredpx = $user->data()->fivehundredpx;
    }

    public function fhpxEndpoint(){ //truncated - this function actually has a number of switch statements
        return $apistring = "https://api.500px.com/v1/photos?feature=user_favorites&username=". $this->fivehundredpx."&sort=rating&image_size=3&include_store=store_download&include_states=voted&consumer_key=I9CDYnaxrFxLTEvYxTmsDKZQlgStJG868GKb"; //this is the line that causes the error
    }

}

when fhpxEndpoint() is called I get the Fatal error: Using $this when not in object context message. 当调用fhpxEndpoint()时,出现致命错误:不在对象上下文消息中时使用$ this。

However if I add another method into the class: 但是,如果我在类中添加另一个方法:

public function userData(){
    print 'here is some text '. $this->fivehundredpx;
}

and call this from outside the class it prints out here is some text jinky32 as expected. 并从类打印出来的类外部调用它,如预期的那样是一些文本jinky32。

Any help much appreciated! 任何帮助,不胜感激!

edit as per request: method is called via 根据请求进行编辑:方法通过调用

$fivehundredpx = new Fivehundredpx;
$obj = $fivehundredpx->fhpxApiConnect($fivehundredpx->fhpxEndpoint());

fhpxApiConnect() is: fhpxApiConnect()是:

public function fhpxApiConnect($apiString){
    print $apiString;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$apiString);
    curl_setopt($ch , CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
etc etc

printing out $apiString as I do in fhpxApiConnect() shows that the url has been constructed correctly and the name jinky32 has been inserted correctly but i still get the fatal error 像我在fhpxApiConnect()中那样打印出$ apiString,表明该URL已经正确构建并且名称jinky32已正确插入,但是我仍然遇到致命错误

EDIT: OK, I've played around and found a way to get it to work. 编辑:好的,我玩过,找到了一种使其工作的方法。 If I change the class variable to 如果我将class变量更改为

public static $fivehundredpx;

and then set it 然后设置

public function __construct(User $user){
    self::$fivehundredpx=$user->data()->fivehundredpx;
}

I can then access it in fhpxEndpoint() using self::$fivehundredpx rather than $this->fivehundredpx 然后,我可以使用self :: $ fivehundredpx而不是$ this-> fivehundredpx在fhpxEndpoint()中访问它

Try this: 尝试这个:

public function userData(){
    print 'here is some text '. Fivehundredpx::fivehundredpx();
}

or can try with: 或者可以尝试:

public function userData(){
    $obj = new Fivehundredpx();
    print 'here is some text '. $obj->fivehundredpx();
}

Based on your comment, you can't access to a property of User like this: 根据您的评论,您将无法访问用户属性,如下所示:

$user->data()->fivehundredpx;

While method data of class User doesn't return your object. 而User类的方法数据不会返回您的对象。 Add to method data: 添加到方法数据:

return $this;

at the end. 在末尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PHP 致命错误:不在 object 上下文中时使用 $this - PHP Fatal error: Using $this when not in object context “致命错误:在不在对象上下文中时使用$ this”时使用require - PHP - “Fatal error: Using $this when not in object context” when using require - PHP PHP错误:致命错误:不在对象上下文中时使用$ this - PHP Error: Fatal error: Using $this when not in object context PHP,致命错误:未捕获错误:不在对象上下文中时使用$ this - PHP, Fatal error: Uncaught Error: Using $this when not in object context in Slim PHP:致命错误:不在对象上下文中时使用$ this - Slim PHP : Fatal error: Using $this when not in object context PHP致命错误:脚本不在对象上下文中时使用$ this - PHP Fatal error: Using $this when not in object context for script 致命错误:不在对象上下文中时使用$ this-PHP,PDO,会话, - Fatal error: Using $this when not in object context - PHP, PDO, Sessions, PHP OOP MVC致命错误:不在对象上下文中时使用$ this - PHP OOP MVC Fatal Error: Using $this when not in object context PHP。 致命错误:不在对象上下文中时使用$ this - PHP. Fatal error: Using $this when not in object context PHP致命错误:不在对象上下文中时使用$ this…用于redirect() - PHP Fatal Error: Using $this when not in object context… Used for redirect()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM