• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to go about using frequent functions

#1
[eluser]Unknown[/eluser]
I'm experiencing codeigniter for the first time, yet I've been programming in PHP since '02 or so. I have gone through some tutorials, and get the basic structure and I love it!

As a first-dev I'm going to throw up basic "user system" functionality. How is it preferred to go out about using frequently used functions/ classes such as a User class? Should you extend the User class on every page that you will use it? as such: (note this isn't my actual code, just testing theory.

controllers/user.php
Code:
class User extends CI_Controller
{
    function __construct() {
        parent::__construct();
    }

    function auth_level() {
        //gets users info and returns auth level
    }
}

controllers/homepage.php
Code:
class Homepage extends User
{
    function __construct() {
        parent::__construct();
    }

    function index() {
        if($this->auth_level() > 2) {
            echo "admin";
        }
    }
}

#2
[eluser]PhilTem[/eluser]
Well, there are two ways to solving your problem. Number one would be something called a MY_Controller which you kinda already created with your user-controller. However, to allow working with CI you should call it MY_Controller. Have a look at the user's guide.
Number two would be to use a library or a helper, that you autoload and then can use in every controller, model, view, library, ...

#3
[eluser]Aken[/eluser]
A library and/or model makes more sense for user management, personally.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.