# Using classes

**URL:** <https://forum.kirupa.com/t/using-classes/185289>\
**Category:** Uncategorized\
**Created:** [April 12, 2006, 7:29am UTC](https://forum.kirupa.com/t/using-classes/185289 "2006-04-12T07:29:57Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![melvijin](https://avatars.discourse-cdn.com/v4/letter/m/e9a140/32.png) [@melvijin](https://forum.kirupa.com/u/melvijin)\
**Post date:** [April 12, 2006, 7:29am UTC](https://forum.kirupa.com/t/using-classes/185289/1 "2006-04-12T07:29:57Z")

</div>

my problem is how to join 2 classes… i wanted to use the other class inside another class… example

```php
<?
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…

tnx… ![](http://webgroundz.com/forum/html/emoticons/biggrin.gif) rok on!!!

---

<div class="post-metadata">

**Author:** ![rufusW](https://avatars.discourse-cdn.com/v4/letter/r/5e9695/32.png) [@rufusW](https://forum.kirupa.com/u/rufusW)\
**Post date:** [April 12, 2006, 10:05am UTC](https://forum.kirupa.com/t/using-classes/185289/2 "2006-04-12T10:05:44Z")

</div>

Hi

I don’t altogether understand your problem.

You can either call the $ball-\>multiply method or make the ball a member of the box.

```php
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.

---

<div class="post-metadata">

**Author:** ![Voetsjoeba](https://avatars.discourse-cdn.com/v4/letter/v/ac91a4/32.png) [@Voetsjoeba](https://forum.kirupa.com/u/Voetsjoeba)\
**Post date:** [April 12, 2006, 10:15am UTC](https://forum.kirupa.com/t/using-classes/185289/3 "2006-04-12T10:15:44Z")

</div>

I don’t think PHP allows for the $box-\>ball-\>multiply(); syntax. I’ve tried doing it as well and was forced to do

```php

$ball =& $box->ball;
$ball->multiply();

```

But that’s possibly cause my server isn’t running the most recent PHP version … not sure.

---

<div class="post-metadata">

**Author:** ![kortexvfd](https://avatars.discourse-cdn.com/v4/letter/k/f1d935/32.png) [@kortexvfd](https://forum.kirupa.com/u/kortexvfd)\
**Post date:** [April 12, 2006, 11:36am UTC](https://forum.kirupa.com/t/using-classes/185289/4 "2006-04-12T11:36:28Z")

</div>

sounds like you may want to extend one of the classes with the other? Making the methods from one avaiable in the other.

---

<div class="post-metadata">

**Author:** ![rufusW](https://avatars.discourse-cdn.com/v4/letter/r/5e9695/32.png) [@rufusW](https://forum.kirupa.com/u/rufusW)\
**Post date:** [April 12, 2006, 11:46am UTC](https://forum.kirupa.com/t/using-classes/185289/5 "2006-04-12T11:46:04Z")

</div>

> [@Voetsjoeba](#):
>
> I don’t think PHP allows for the $box-\>ball-\>multiply(); syntax.

It allows me to do it in PHP 4.3.10

---

<div class="post-metadata">

**Author:** ![skOOb](https://avatars.discourse-cdn.com/v4/letter/s/8dc957/32.png) [@skOOb](https://forum.kirupa.com/u/skOOb)\
**Post date:** [April 12, 2006, 11:55am UTC](https://forum.kirupa.com/t/using-classes/185289/6 "2006-04-12T11:55:45Z")

</div>

> [@kortexvfd](#):
>
> sounds like you may want to extend one of the classes with the other

Exactly what it sounds like…look on [www.php.net](http://www.php.net) for ‘extends.’  
From [php.net](http://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. \*

---

<div class="post-metadata">

**Author:** ![rufusW](https://avatars.discourse-cdn.com/v4/letter/r/5e9695/32.png) [@rufusW](https://forum.kirupa.com/u/rufusW)\
**Post date:** [April 12, 2006, 12:49pm UTC](https://forum.kirupa.com/t/using-classes/185289/7 "2006-04-12T12:49:30Z")

</div>

> [@melvijin](#):
>
> my problem is how to join 2 classes… i wanted to use the other class inside another class…

To use a class ‘inside another class’ you would use composition

My understanding is that to use extends suggests that the extended class is a specialized type of the parent class :-/

Much like the ‘Is A’ vs ‘Has A’ argument in Colin Moock’s Actionscript 2.0 book.

To extend it you’d make a generic ‘shape’ class which had a multiply method and then extend both the box and the ball from that class.

Which has it’s benefits.

:beam:

---

<div class="post-metadata">

**Author:** ![kortexvfd](https://avatars.discourse-cdn.com/v4/letter/k/f1d935/32.png) [@kortexvfd](https://forum.kirupa.com/u/kortexvfd)\
**Post date:** [April 12, 2006, 1:09pm UTC](https://forum.kirupa.com/t/using-classes/185289/8 "2006-04-12T13:09:38Z")

</div>

Sounds like either might work:

For Composition See:  
[http://www.devshed.com/c/a/PHP/Object-Interaction-in-PHP-Introduction-to-Composition-conclusion/](http://www.devshed.com/c/a/PHP/Object-Interaction-in-PHP-Introduction-to-Composition-conclusion/)

For Extends See:  
[http://us2.php.net/manual/en/language.oop5.basic.php](http://us2.php.net/manual/en/language.oop5.basic.php)
