I'm trying to call an object using method and I got stuck.
Lets suppose that I have a class named Entity
which used to pseudo-struct-type variable.
The object Entity
is usually have multiple relation among other Entity
instances. So, I make a class EntityModule
to handle database save, load, and possibly other handy methods.
What I'm trying to do is..
Lets if I want to insert Entity
object to the table.
With a method Entity->save()
, I want to call EntityModule->saveEntity()
method from pre-exist EntityModule
obejct.
How should I get this? I think, just using other instantiated object inside a class, its very weired.
.. Its very confusing for amateur.. Following is my example.
<?php
class Container
{
public function __get($varName)
{
if(!isset($this->varName)) {
// Can it be more simpler?
switch($varName) {
case 'PDO':
$this->PDO('dbdbdb');
break;
case 'SessionStorage':
$this->SessionStorage($this->PDO);
break;
default:
break;
}
}
}
// This is weird method.. can it be more simpler?
public function __call($method, $args) {
$n = count($args);
if($n === 0) {
$this->$method = new $method();
} elseif($n === 1) {
$this->$method = new $method($args[0]);
} elseif($n === 2) {
$this->$method = new $method($args[0], $args[1]);
} elseif($n === 3) {
$this->$method = new $method($args[0], $args[1], $args[2]);
} elseif($n === 4) {
$this->$method = new $method($args[0], $args[1], $args[2], $args[3]);
} elseif($n === 5) {
$this->$method = new $method($args[0], $args[1], $args[2], $args[3], $args[4]);
} elseif($n === 6) {
$this->$method = new $method($args[0], $args[1], $args[2], $args[3], $args[4], $args[5]);
} elseif($n === 7) {
$this->$method = new $method($args[0], $args[1], $args[2], $args[3], $args[4], $args[5], $args[6]);
};
}
}
?>
Using the container, I try to instantiate object dynamically.
class Schema
{
public $id;
public $type;
public $name;
public $desc;
public function __construct(int $id = false, $type = 0, $name = 'Temporal schema name', $desc = 'No description.')
{
$this->id = $id;
$this->type = $type;
$this->name = $name;
$this->desc = $desc;
return;
}
public function save()
{
if(!isset($ctn->SchemaModule)) {
// This is another weird part.
// Is this the right way to make a class? Call a random instance inside the class? Doesn't it wierd?????????? ???? ?.?? ??? mentalbreak.
$ctn->SchemaModule->save($this);
}
}
}