[SOLVED] class within a class

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

[SOLVED] class within a class

Post by dull1554 »

if you have a class within a class
i.e.

Code: Select all

class test{
class test2{
}
}
when i initiate test (new test) does it also initiate test2 or would i have to do a "new test->test2"?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

dunno offhand.. it wouldn't in C last I used nested classes.. can always try it:

Code: Select all

class test
{
  function test()
  {
    echo "in test()\n";
  }

  class test2
  {
    function test2()
    {
      echo "in test2()\n";
    }
  }
}
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Are you referring to PHP5?

PHP4 doesn't allow nested classes...
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

I think you can't do it in PHP 4. I'm not playing with PHP 5 so...

Scorphus
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post by PrObLeM »

Code: Select all

class test2 extends test1
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

would that work in php 4?
or if im in php5 do i still need to extend it to the outter class?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

extends works in 4 and 5
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

so using extends, it is possiable to nets classes, but if in 5 you car able to do so without extending the class?

do i have it right or is this way over my head?!?!?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

nested classes, as your original post has them, isn't extending the parent. The nested class is a component of its parent.. like how a function is apart of its parent class.
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

so it would be more like this.....

Code: Select all

class test{
} 
class test2 extends test{
function test3(){echo "test4";}
}

//then
$test = new test;
$test->test2->test3();
after typing this i just tried it and i get an error, what would i have to do to get this to work, i guess im totally missing the point.....
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

class test
{
  function somefunc()
  {
    echo "joy!";
  }
}

class test2 extends test
{
  function somefunc()
  {
    echo "juice!\n";
    test::somefunc();
  }
}

$blah = new test;
$blah2 = new test2;
or something.. (not tested)
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Use inheritance if the two classes are genuinely of the same "type". Suppose you had a Rank class used for adding a ['rank'] key to search results. You might extend this with derived classes which define different types of ranking algorithmns.

It's usually better not to use inheritance if you have another option, ie aggregation or composition.

Code: Select all

<?php
/*
    Aggregation:
*/
class Test 
{
    /*
        param (object)
    */
    function Test(&$test2)
    {
        $this->test2 =& $test2;
    }

    /*
        etc..
    */

}


/*
    Composition:
*/
class Test 
{

    function Test()
    {
        $this->test2 =& new Test2;
    }

    /*
        etc..
    */

}

?>
Incidentally, that last one is known as the Factory pattern - see the design section at http://www.phppatterns.com.
Last edited by McGruff on Mon Aug 08, 2005 7:42 pm, edited 1 time in total.
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

allright, cool, i think i got it now and thanks for all the help
Post Reply