my problem is how to join 2 classes… i wanted to use the other class inside another class… example
<?
class box {
var $arg1;
var $arg2;
function box($arg1,$arg2) {
$this->arg1=$arg1;
$this->arg2=$arg2;
}
function add() {
return $this->arg1+$this->arg2;
}
}
class ball {
var $arg3;
var $arg4;
function ball($arg3,$arg4) {
$this->arg3=$arg3;
$this->arg4=$arg4;
}
function multiply() {
return $this->arg3 * $this->arg4;
}
}
?>
now here is the part…
i wanted to have another function inside the class box where i can add and multiply and the multiplication method is by using the class ball… all in all, only 2 classes are used…
You can either call the $ball->multiply method or make the ball a member of the box.
class box {
var $arg1;
var $arg2;
var $ball; //a ball object
function box($arg1,$arg2,$ball) {
$this->arg1=$arg1;
$this->arg2=$arg2;
$this->ball=$ball;
}
I believe it’s called ‘composition’.
so you would call $box->ball->multiply()
You would have to alter your multiply method so it can accept arguments.
Exactly what it sounds like…look on www.php.net for ‘extends.’
From php.net: *Often you need classes with similar variables and functions to another existing class. In fact, it is good practice to define a generic class which can be used in all your projects and adapt this class for the needs of each of your specific projects. *