Welcome Guest, Not a member yet? Register   Sign In
[Solution] Extending libraries: proper locations for the Classname and MY_Classname library files
#1

[eluser]aidehua[/eluser]
This is probably second nature to most of you, but I've just spent an hour trying to figure it out so I thought I'd post it here in case it might save someone else five minutes down the line.

(I was using Simon Maddox's Twitter class (available at http://github.com/simonmaddox/codeigniter-twitter), but I think this would apply to any library class in CodeIgniter.)

First up I downloaded the class file (Twitter.php) and saved it in my application/libraries folder.

I was able to load it successfully from the controller in the usual way:

Code:
$this->load->library('twitter');

But then I wanted to extend the class a little bit, so I created a new class and called it MY_Twitter.php (as documented at http://ellislab.com/codeigniter/user-gui...aries.html).

I saved my new class in the application/libraries folder.

I then tried to load the extended library from the controller, again as per the documentation, in the normal way:

Code:
$this->load->library('twitter');

This returned an error: "Unable to load the requested class: Twitter"

After much experimenting, I found the solution: I moved the Twitter.php file out of the application/libraries folder, and into the system/libraries folder, so that I now had:

codeigniter/system/libraries/Twitter.php
codeigniter/application/libraries/MY_Twitter.php

It now works just fine.

I wonder if the documentation might make more explicit the following point:

"3rd-party" library classes should be saved in the system/libraries folder. (Extensions to 3rd-party library classes, using the MY_ prefix, should be saved in the application/libraries folder.

PS This post is about library classes in general, not specifically about this Twitter class. But, for what it's worth, my new class looked like this:

Code:
<?
class MY_Twitter extends Twitter {

/* Edit the user and pass variables as appropriate */
    public $user = '[twitterusername]';
    public $pass = '[password]';

function get_current_status($status){
/* Returns the user's current status message and the date/time it was created */
    $status = $this->user_timeline($this->user,2);
    
    return array(
                'text'=>$status[0]->text,
                'created_at'=>$status[0]->created_at
                );
}

function tweet($thetweet){
/* Makes tweeting as easy as saying tweet("Oh what a lovely morning") */
    $this->auth($this->user,$this->pass);
    $this->update($thetweet);
}

}
#2

[eluser]pistolPete[/eluser]
Extending a class using the MY_ prefix is reserved to core classes only: http://ellislab.com/codeigniter/user-gui...asses.html

If you need to extend a library which resides in ./application/libraries/ you either need to autoload the base class or use standard PHP require().
#3

[eluser]aidehua[/eluser]
Thanks for clarifying.

Are you saying you advise AGAINST putting a 3rd-party library (like, in my example, Simon Maddox's Twitter.php) into ./system/libraries/?

Or, if taking the require() route, do you mean requiring the Someclass.php file from within the MY_Someclass.php file, like this?

Code:
<?php
require ('Someclass.php');

class MY_Someclass extends Someclass{

function someFunction(){

}

}

Then presumably in the controller you'd have to load the MY_ class rather than the original:

Code:
$this->load->library('MY_Someclass');




Theme © iAndrew 2016 - Forum software by © MyBB