Welcome Guest, Not a member yet? Register   Sign In
Php class
#1

[eluser]nuclearmaker[/eluser]
hi, i want to create the some CI library,
so here is my code.
but 2 diff method, i need to call the instance again.is there a way to call once only?

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

require_once('SomeClass.php');

public function title()
{
    $html = new SomeClass();
    
    return $html->title;
}

public function body()
{
    // here i need to call the instance again.is there a way to call once only?
    $html = new SomeClass();
    
    return $html->body;
}
#2

[eluser]LuckyFella73[/eluser]
I guess you have to load the library in your class contructor:
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Yourclass
{
    public function __construct()
    {
        require_once('SomeClass.php');
        $this->html = new SomeClass();
    }

    

    public function title()
    {
        return $this->html->title;
    }

    public function body()
    {
        return $this->html->body;
    }

}
#3

[eluser]danmontgomery[/eluser]
Libraries in CI are just classes, why not stick it in the libraries folder and use the loader?

Code:
class Yourclass
{
    public function __construct()
    {
        $CI = get_instance();
        $CI->load->library('html');
        $this->html = $CI->html;
    }

    

    public function title()
    {
        return $this->html->title;
    }

    public function body()
    {
        return $this->html->body;
    }

}




Theme © iAndrew 2016 - Forum software by © MyBB