This is what I have in foo.php
class Foo
{
public $foo = NULL;
public $foo2 = NULL;
public function setFoo ($foo, $foo2)
{
$this->foo = $foo;
$this->foo2 = $foo2'
}
}
This is what I have in foo3.php
class Foo3 extends Foo
{
public $foo3 = NULL;
public function setFoo3 ($foo3)
{
$this->foo = $foo3;
}
}
This is how I require it in my third file run.php:
require_once "foo.php";
require_once "foo3.php";
$foo = new Foo();
$foo->setFoo3("hello");
I get this error:
Fatal error: Call to undefined method Foo::setFoo3()
I'm not sure if the problem is how I'm requiring them. Thanks.
In your example, you are instantiating
Foo
, which is the parent and has no knowledge of the methodsetFoo3()
. Try this:Of course that doesn't work. You've created a
Foo
object, and then tried to callfoo3
on it. ButFoo
doesn't have afoo3
method.At the first, in your foo.php shouldn't mark your fields
public
, because you set those values insidesetFoo($foo1, $foo2)
method. Instead, you may have something like:Then you should add
extends
keyword when declaring classFoo3
, and another thing you need to include extending class file in the beginning of the file. In your case you may have something like the following in your foo3.php file:then you can create an instantiate of a
Foo3
class in your run.php like so:and my advice, you should read a little about OOP techniques ;)