jmathai / epiphany

A micro PHP framework that's fast, easy, clean and RESTful. The framework does not do a lot of magic under the hood. It is, by design, very simple and very powerful.
https://github.com/jmathai/epiphany
Other
686 stars 139 forks source link

Not able to access the functions inside my class #99

Closed imarshmultani closed 9 years ago

imarshmultani commented 9 years ago

suppose i have the following code :

class User {

 private $myvar;

function __construct(){

$myvar = 'some data';
$this->myvar = 'some data';
}

// the below function is called from router
public static function router_called_function(){
echo $myvar;
echo $this->myvar;
}
}

so as per the code when action 'router_called_function' is called i have to access the object : $myvar or $this->myvar but it does not let me use it , it says :

Fatal error: Using $this when not in object context in

jmathai commented 9 years ago

When you call a method statically you can't use $this as there's no object context. You'll need to instantiate User first. Not sure if you meant to define the function statically or not.

$userObj = new User;
echo $userObj->router_called_function();
imarshmultani commented 9 years ago

but as i have described the problem , is it possible to avoid static ? and use normal public function or i have too call the class again n again inside functions to connect with other functions ? inside the same class?

i have an User class which contains too many functions, not all are being called from the router , most of them are to save data , get data , filtering and much more which i need to use inside my function called : facebook_login which is connected to router for executing the facebook login and then when the action is triggered from the url i also need to connect with facebook's api class by extending it or calling it inside : __construct function ,, but that always not let me do that.

georules commented 9 years ago

You can avoid using static if you instantiate an object of the class and then call the function from that object.