Welcome Guest, Not a member yet? Register   Sign In
question regarding 2 classes
#1

[eluser]stitchesinmyhead[/eluser]
hi,

i need help. is it possible that i can re-use the function that i already created? the two functions are located in different files. please see my code below.

Code:
a.php
class a extends Model
{
function getB()
{
$this->b->hello();
}
}

b.php
class b extends Model
{
function hello()
{
  return "hello from b.php";
}
}

i'm having this error in a.php Severity: Notice

Message: Undefined variable: b
#2

[eluser]LuckyFella73[/eluser]
You could extend the base model

Code:
// MY_model.php
class MY_Model extends CI_Model // CI 2 -- "class MY_Model extends Model" in CI < 2.0
{
//yourfunctions here
}

Save in application/core (CI 2) or application/libraries (CI < 2.0)

There is a nice base model available - provided by Phil Sturgeon:
http://bitbucket.org/philsturgeon/codeig...base-model

You could place corresponding functions in there. Don't know if that make sense
in your project. In your example class b is not in the scope of class a.
#3

[eluser]WanWizard[/eluser]
You can only use 'b' if you load 'b' first, either in the controller that calls 'a', or in 'a' itself before you call 'b'.
#4

[eluser]stitchesinmyhead[/eluser]
thank you sir, but i cant understand the instructions. im a newbie kindly please explain further.
#5

[eluser]atebit[/eluser]
[quote author="stitchesinmyhead" date="1291777258"]thank you sir, but i cant understand the instructions. im a newbie kindly please explain further.[/quote]

Prior to accessing "b" you have to load the model.. such as...

$this->load->model('Model_name');

Code:
a.php
class a extends Model
{
function getB()
{
  $this->load->model('b');
  $this->b->hello();
}
}

b.php
class b extends Model
{
function hello()
{
  return "hello from b.php";
}
}
#6

[eluser]stitchesinmyhead[/eluser]
Thank you so much brother!




Theme © iAndrew 2016 - Forum software by © MyBB