0

I have large class foo, that I want to split to two separate classes (and files);

My class foo uses contruct function and noumerous $this references.

I need a second class bar to be an extension for original foo class, so I could still use contruction with additional parameter if to include bar class;

$includeBar = true;
$foo = new foo($config, $includeBar);

I've tried putting it this way:

Class bar extends foo {

   public function barFunction(){
      //some function of bar
   }
}


Class foo {

   public function __construct($config, $includeBar = true) {
      if ($includeBar) {
         include_once 'bar.php';
      }
   }

}

But when I call:

$foo = new foo($config, true);
$foo->barFunction();

It fails, saying

PHP Fatal error: Uncaught Error: Call to undefined method foo::barFunction()

What am I doing wrong? pls help, got stuck

7
  • In which page are you calling $foo = new foo($config, true); ? show us the includes from it. Commented Jan 8, 2018 at 1:32
  • 1
    What is the use case for this? Why not instead of passing $includeBar conditionally instantiate an instance of bar rather than foo when needed? Commented Jan 8, 2018 at 1:32
  • @aendeerei thats the whole script. I just initialize foo class and expect functions from bar class to be available as well as functions from foo; Commented Jan 8, 2018 at 1:35
  • 3
    it is simply the other way round. foo should extend bar. And then, when instanciating the class, call the extending one (foo). Then there's no need for a param $includeBar Commented Jan 8, 2018 at 1:42
  • 1
    In general, if you include a file inside a function, the code found in the file is callable only inside the function, not outside of it. Commented Jan 8, 2018 at 1:51

2 Answers 2

2

It should be the other way round.
Bar is your base class, that contains all the methods that every sub class also needs. Foo is the extention, the extras, so Foo extends Bar.

<?php 

// file bar.php
Class Bar {
    public function __construct($config) {
         $this->config = $config;
    }

    public function barFunction() {
        echo "I'm everybody ".$this->config['msg'];
    }
}

// file foo.php
require_once('bar.php');

Class Foo extends Bar {
    public function fooOnly() {
        echo "I'm foo ".$this->config['msg'];
    }   
}


// consuming file index.php
include('foo.php');
$config = array('msg'=>'and I need coffee');
$foo = new foo($config);
$foo->barFunction();  // we can call this, because foo extends bar

// this won't work:
$bar = new Bar($config);
$bar->fooOnly();

// but this:
$bar->barFunction();
$foo->fooOnly();

(all the includes/requires can be omitted when using a proper autoloader!)

Sign up to request clarification or add additional context in comments.

Comments

0

If you want reuse class methods or separate class method implementation I think you can use trait and then you can use keyword use to require functions to your class.

For example:

<?php
class Base {
    public function sayHello() {
        echo 'Hello ';
    }
}

trait SayWorld {
    public function sayHello() {
        parent::sayHello();
        echo 'World!';
    }
}

class MyHelloWorld extends Base {
    use SayWorld;
}

$o = new MyHelloWorld();
$o->sayHello();
?>

The above example will output:

Hello World!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.