Welcome Guest, Not a member yet? Register   Sign In
extending libraries
#1

[eluser]stretchnate[/eluser]
is it possible to extend custom libraries from other custom libraries?

Example:
we are porting some libraries over to CI from an attempt at Zend Server (which I'm very excited about) but I'm having trouble getting a custom library that, in zend server, extended another custom library, to do the same in CI. Both files are in the /application/libraries/ folder and both are named appropriately but when I run the code I get "Class 'Companyresultobject' not found in /Applications/MAMP/htdocs/Ledger.Local/system/application/libraries/rouuid.php on line 3"

/application/libraries/Rouuid.php
Code:
class Rouuid extends Companyresultobject {
    public $uuid;

    function Rouuid($params) //$params = array($status = 0, $uuid=null, $message='')
    {
        parent::Companyresultobject();

        $debug = debug_backtrace();
        if( ! empty( $debug[0]['function'] ) ) {
            $this->method = $debug[0]['function'];
        }

        $this->status = $params['status'];
        if( isset($params['uuid']) ) {
            $this->uuid = $params['uuid'];
            $this->data = $params['uuid'];
        }

        if( ! empty($params['message']) ) {
            $this->messages[] = $params['message'];
        }
    }
}

/application/libraries/Impactresultobject.php
Code:
<?php

class Companyresultobject
{
    public $status;

    public $data;
    public $messages;
    public $method;
    
    public function Companyresultobject($status = 0, $data=null, $message='')
    {
        $debug = debug_backtrace();
        if( ! empty( $debug[0]['function'] ) ) {
            $this->method = $debug[0]['function'];
        }
        
        $this->status = $status;
        if( isset($data) ) {
            $this->data = $data;
        }
        
        if( ! empty($message) ) {
            $this->messages[] = $message;
        }
    }
    
    public function getMessageString()
    {
        return implode(", ", $this->messages);
    }


    public function __toString()
    {
        $data = get_object_vars($this);
        return print_r($data, true);
    }
    
    
}

Alternatively is it possible to create a core library that can be extended in a similar fashion?

Thanks in advance.
#2

[eluser]slowgary[/eluser]
CodeIgniter only loads files that you tell it to load. You'll likely need to do something like this:
Code:
$this->load->library('parent');
$this->load->library('extends_parent');

$this->extends_parent->parent_method();

If, however, you only ever plan to use the libraries together you could just put them both in one file:
Code:
<?php
// this file is apple.php
class Apple extends Fruit
{
}

class Fruit
{
}

Now in your controller (or wherever you need to use these, you can just load the Apples class and you'll also be loading class Fruit. Alternatively, if you plan to have many different classes that extend Fruit (like Oranges, Bananas, Pineapples) but you don't want to load the Fruit class everytime, you can throw the Fruit class into your autoload.php config file or just require_once('fruit.php') in each of the classes that extends it.

I hope this helps.
#3

[eluser]Rolly1971[/eluser]
so you have 2 libraries, both in the applications/libraries:

/application/libraries/Rouuid.php (class Name: Rouuid)

/application/libraries/Impactresultobject.php (class name: Companyresultobject)

first, add companyresultobject to your autoload['libraries'] in config.php

then rename:

Impactresultobject.php

to:

Companyresultobject.php

---

CI expects classes to have the same filename as the name of the class, and in the case of libraries, the first letter in the filename and class name should be capitalized.
#4

[eluser]stretchnate[/eluser]
Fantastic, thanks for the replies. It worked like a dream.




Theme © iAndrew 2016 - Forum software by © MyBB