0

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.

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.

Any help much appreciated!

edit as per request: method is called via

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

fhpxApiConnect() is:

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

EDIT: OK, I've played around and found a way to get it to work. If I change the class variable to

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

6
  • Please post the code where you actually call fhpxEndpoint(). Commented Jun 12, 2014 at 10:48
  • Does method of class User returns $this? Commented Jun 12, 2014 at 10:52
  • @b4rt3kk do you mean within the constructor? if so no it doesn't Commented Jun 12, 2014 at 11:02
  • You should start by passing in an instance of User when instantiating Fivehundredpx. Right now it should fatal even before you call fhpxEndpoint Commented Jun 12, 2014 at 11:06
  • 1
    @tlenss yes you are right. The Fivehundredpx object is actuallt instantiated elsewhere and does pass in the instance of User. I just added it above in closer proximity to where I am using the instance but forgot to include the User param. Sorry Commented Jun 12, 2014 at 11:21

2 Answers 2

0

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();
}
Sign up to request clarification or add additional context in comments.

1 Comment

doing this means that no longer prints out here is some text jinky32 and generates Fatal error: Call to undefined method Fivehundredpx::fivehundredpx()
0

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. Add to method data:

return $this;

at the end.

1 Comment

I can access the value using $user->data()->fivehundredpx; though. If I couldn't then surely my userData() method (which I am only using to try and debug this issue) wouldn't print out the value since $this->fivehundredpx is set in the constructor by $user->data()->fivehundredpx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.