Welcome Guest, Not a member yet? Register   Sign In
How to use another model function in my model?
#1

[eluser]peekk[/eluser]
Hello, I'm new in CI programming, and I have some issues right now.
I have 2 PHP classes, let's call them class1 and class2.
In class1 I have method, which creates an object of class2 and does a method from class2 on it.
I created a two models, Class1 and Class2 and now I have no idea how can I call methods from Class2 in Class1.

class examples:

Code:
class Class1{
    public function createclass2()
    {
        return new Class2();
    }

    protected function useclass2()
    {
        return $this->createclass2()->class2funct();
    }
}

class Class2{
    public function class2funct(){
     echo 'I did it';
    }

}




Can you suggest me, how to resolve it, maybe should I use libraries? I don't know how to create these class structures that I used to make in traditional PHP, now in CodeIgniter environment.

Thanks in advance.
#2

[eluser]Trankh1[/eluser]
Yes, you should use libraries.
Code:
class SmethingLib {

public function __construct() {
  
}


function yourcommonmethod() {
      echo 'I did it';

}

}

class Class1{
  

    protected function useclass2()
    {

         $this->smethingLib->class2funct();
    }
}

}


Load your library in autoload file or add $this->load->library('smethingLib') before your call
#3

[eluser]peekk[/eluser]
Thanks!

Is there no way to do it using models?




Theme © iAndrew 2016 - Forum software by © MyBB