I know this is done all the time, but for some reason, it’s not making one bit of sense to me. [Insert @#$%! here] I don’t know what the proper OOP procedure is for this (assumingly) obvious solution. I’ve briefly read up on Abstract and Interface type classes, with no avail to any examples that do what I’m asking.
So here’s the setup…
In this example, I have two classes, one that I am going to call from my script, and another two that I could either call from my script or from within the first class that I called. The psuedo-code:
<?php
// Some code here
$timer = new timer();
$doSomethingCool = new doSomethingCool();
$doSomethingCool->doIt();
print $timer->length();
class timer {
private $startTime;
private $stopTime;
private $length;
public function start() {
// Start running the timer
}
public function end() {
// End the timer
}
public function length() {
// Return the length of the timer
return $this->length;
}
}
class doSomethingCool {
public function doIt() {
$timer->start();
// Some code here
$timer->end();
}
}
?>
I have been able to get this to “run” (see below), but this work-around is messy, and I am 100% sure this isn’t proper object oriented modeling:
<?php
// Start by declaring all classes and pass all classes to themselves...
$doSomethingCool = new doSomethingCool();
$timer = new timer();
$class = array(
'doSomethingCool'=>$doSomethingCool,
'timer'=>$timer
);
$doSomethingCool->class = $class;
$timer->class = $class;
// Some code here
$class['doSomethingCool']->doIt();
print $timer->length();
class timer {
// In each class we now declare a public variable
// 'class' that we had passed all class instances to...
public $class;
private $startTime;
private $stopTime;
private $length;
public function start() {
// Start running the timer
}
public function end() {
// End the timer
}
public function length() {
// Return the length of the timer
return $this->length;
}
}
class doSomethingCool {
public $class;
public function doIt() {
$this->class['timer']->start();
// Some code here
$this->class['timer']->end();
}
}
?>
Due to E_STRICT, I do not want to use $timer::start();.
So what’s the solution, ladies and gentlemen? Thanks!
I think you want to inject other classes into some class. If so:
Note that you should add your real classes of course (where I have used
DatabaseandTimer).