...

View Full Version : use another class within one class



xiaodao
10-28-2007, 05:57 PM
Hello

i happen need to use another class inside current class, what shall i do? use normal way like "$k=new image();"?

Thanks

GJay
10-28-2007, 06:09 PM
either that, or pass the instance in either via the constructor or via a 'setter' method. Both of these will result in a less tightly coupled system, which is generally a good thing.

If you provide more information (what are the classes in question?) you'll be more likely to receive a decent answer.

xiaodao
10-28-2007, 06:46 PM
Hi

thanks for reply


$image = new Gimage();
$image->limit="100";

$image->save_file = $thumbpath;
$image->create($newname);

above is the codes i want to use in below class



class zip
{
}



how to write in class "zip"?

GJay
10-28-2007, 07:10 PM
what is zip? how are you using it? what does it have to do with a Gimage?
There are a few different ways:


class Zip {
public function foo() {
$img = new Gimage();
$this->bar = $img->baz();
}
}
//or
class Zip {
public function foo(GImage $img) {
$this->bar = $img->baz()
}
}
//or
class Zip {

protected $img;

public function __construct(GImage $img) {
$this->img = $img;
}

public function foo() {
$this->bar = $this->img->baz();
}

}
//or
class Zip {
protected $img;

public function setGImage(GImage $img) {
$this->img = $img;
}

public function foo() {
$this->bar = $this->img->baz();
}
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum