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
Fivehundredpx. Right now it should fatal even before you callfhpxEndpoint