Code: Select all
class Object1 {
$var = "hello";
function attach($object) {
$this->a = $object;
}
}
class Object2 {
//stuff
}
$o = new Object1();
$o->attach(new Object2());Moderator: General Moderators
Code: Select all
class Object1 {
$var = "hello";
function attach($object) {
$this->a = $object;
}
}
class Object2 {
//stuff
}
$o = new Object1();
$o->attach(new Object2());Code: Select all
class Object1 {
$var = "hello";
function attach($object) {
$this->a = $object;
$this->a->parent($this);
}
}
Code: Select all
class parent_class
{
private $_child;
public $var_test;
public function __construct()
{
$this->_set_a_var();
}
public function attach($child)
{
$this->_child = $child;
}
private function _set_a_var()
{
$this->var_test = 'I am the parent!';
}
}
class child_class
{
public function __construct($parent)
{
$this->_parent = $parent;
$this->_echo_parent_var();
}
private function _echo_parent_var()
{
echo "I'm talking for my mom and she said: {$this->_parent->var_test}<br />";
}
}
$parent = new parent_class();
$parent->attach(new child_class($parent));
Yeah I'm not really worried about it. I am more concerned with giving onion2k a solution that is acceptable to him.Mordred wrote:"child/parent" is a metaphor normally used with class inheritance. "attach/owner" sounds better.
Code: Select all
class Object1 {
function attach($object) {
$this->plugin = $object;
$this->plugin->registerOwner($this);
}
}
class Object2 {
function registerOwner($owner) {
$this->_owner = $owner;
}
}Code: Select all
class foo
{
public function __construct()
{
$this->kernel = kernel::get_instance();
}
public function blah()
{
# just for clarification: get_page(database query, current page, results per page)
# returns an array containing records for page 1 or false on failure
if (false === ($records = $this->kernel->pagination->get_page("select a, b, c, d from users where active = '1'", 1, 50)))
{
# we set a status message
$this->kernel->status->error("There was an error processing your request.");
# this query shouldn't have failed so log this
$this->kernel->system_log->add_entry(get($class), __FILE__, __LINE__, "unexpected database query failure", 'E_USER_ERROR');
return false;
}
return $records;
}
}
Code: Select all
class Object1 {
function attach($object) {
$this->plugin = $object;
}
function doSomething() {
$this->whatIWant = $this->plugin->something($this->whatObject2Needs);
}
}So, basically you've invented global functions with lots of -> thrown in.astions wrote:Yeah either way. You just need to get an instance of it over the other class somehow. In my framework I use a kernel that in addition to a number of other things, holds an object of every class. So I can call any method of any class from any place and it saves a ton of memory PLUS it's very quick.
Code: Select all
$image =& new Image(IMAGE_BASE.DIRECTORY_SEPARATOR."drops.jpg");
$image->attach(new image_fx_resize(100,100));
header("Content-type: image/jpg");
$image->imageJpeg();That's just the way I write filename paths. If the image was in the same directory as the script then "drops.jpg" would work fine. $_FILES['upload']['tmp_name'] also works.astions wrote:Looks good. I would try to write so that I wouldn't have to pass the "IMAGE_BASE.DIRECTORY_SEPARATOR." in the image path argument. Not sure if that would fit in your coding style or not.