tags:

views:

132

answers:

2

I have a user class with static methods getById and getByUsername

I have the class in the application/libraries folder

How do I call the classes from a controller?

Theory 1:

$this->user = new User();
$this->user::getById;

Theory 2:

$user = new User();
$user::getById;

or is there a clean way of doing it much like how Kohana helpers do it; much like:

text::random();


here's what I am trying to accompplish:

I want to call a static mehthod in the user library from my controller

In PHP you usually include the file (User.php) and the static methods are ready

User:getById

but then how would I do the same thing in an MVC framework?

shall I do an include too?

Like include ('User.php');?

+1  A: 
User::getById();

and

User::getByUserName();

Edit: In response to your question edit, generally frameworks have an auto-loading mechanism that will find and load a class file for you once you reference that class. So when you type User::getById(), the PHP interpreter will see that it needs to load the User class (if it hasn't been loaded already), and run the autoloading procedure to find the correct code to include.

I've never used Kohana, but I would be quite surprised if it didn't use some form of autoloading. If it does not, then yes, a simple include('User.php') will be enough to make the static method calls to User work.

zombat
with that said, how do I create an instance of the User class in a constructor? I certainly cannot do it with $User = new User, right?
No, you cannot create a new User inside the constructor of a User object, otherwise you would have an infinite loop of User object creation. You could create it in any other constructor with `$user = new User()` though. What are you trying to accomplish?
zombat
A: 

The confusing thing is Kohana's convention of writing "helper" classes with lowercase names.

Your user php file will probably all ready be loaded if your using it as a model, so you can use zombat's suggests of User::getById();.

I don't like to follow their naming convensions for helpers or libraries and instead do:

require_once(Kohana::find_file('libraries', 'user_utils', TRUE, 'php'));
Ozten