Hey thanks for reading this.

Say I have 3 classes and I want to combine those class into a core.
Example

<?php

class One {
    
    public function get_me(){
        echo 'Hello from class One. ';
    }
    
}

class Two {
    
    public function call_me(){
        echo 'Hello from class Two. ';
    }
    
}

class Three {
    
    public function leave_me(){
        echo 'Hello from class Three. ';
    }
    
}

class Core {
    
    public $one;
    public $two;
    public $three;
    
    public function __construct(){
        $this->one = new One;
        $this->two = new Two;
        $this->three = new Three;
    }
    
}

$core = new Core;
$core->one->get_me();
$core->two->call_me();
$core->three->leave_me();

?>

This outputs

Hello from class One. Hello from class Two. Hello from class Three.

Normally all these classes would be in different files but you get the point.
I want to know if there is a way to use class One and class Two and class Three 's methods in class Core as if it was it's own so I can use $code->get_me(); instead of $code->one->get_me(); without using class Core extends One because I want to extend them all.

Any help?

Recommended Answers

All 3 Replies

Looks cool. Thanks for you help :D

For those who are wondering: this is how I implemented it my previous code:

<?php

class One {
	
	public function get_me(){
		echo 'Hello from class One. ';
	}
	
}

class Two {
	
	public function call_me(){
		echo 'Hello from class Two. ';
	}
	
}

class Three {
	
	public function leave_me(){
		echo 'Hello from class Three. ';
	}
	
}

class Core {
	
	public $one;
	public $two;
	public $three;
	
	public function __construct(){
		$this->one = new One;
		$this->two = new Two;
		$this->three = new Three;
	}
	
	public function __call($name,$arguments){
		$obj = array('one','two','three'); // Array of object names. Important ones first. Just in case.
		foreach($obj as $object){
			if(method_exists($object,$name)){
				return($this->$object->$name(implode(',',$arguments))); // Send over to the object.
			}
		}
	}
	
	public function get_me(){
		echo 'You can\'t get through to get_me class One.';
	}
	
}

$core = new Core;
$core->get_me();
$core->call_me();
$core->leave_me();

?>

Reading some other articles, however, some people used call_user_func() and call_user_func_array() instead. This technique, I have read is slow. I'm not sure however of the speed compared to $this->$object->$name(implode(',',$arguments)) .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.