Page 1 of 1

calling a class from within a class

Posted: Thu May 25, 2006 10:52 am
by Luke
Can you do something like this? (I know this doesn't work because I tried it, but is it possible using correct syntax)?

Code: Select all

class someClass($val, $classInsance){
    function someClass($val, $instance){
        $this->val = $val;
        $this->class = $insance;

        $this->class->method('bla bla');
    }
}
I hope this question makes some sense...

Posted: Thu May 25, 2006 10:59 am
by timvw
Offcourse it's possible..

Code: Select all

<?php
class A {
  function foo();
}

class B {
  var $a;

  function B($a) {
    $this->a = $a;
  }

  function bar() {
    $this->a->foo();
  }

$a = new A;
$b = new B($a);
$b->bar();
Be aware that with PHP4 the 'objects' are copied though... (you would need to pass the objects by reference & if you don't want this)

Posted: Thu May 25, 2006 11:01 am
by JayBird
Can also do this i think

Code: Select all

<?php
class A {
  function foo();
}

class B {
  function bar() {
    $a = new A;
    $a->foo();
  }


$b = new B;
$b->bar();
Just depend on how your using your classes

Posted: Thu May 25, 2006 11:50 am
by infolock
many ways...

Code: Select all

# Using extends : 

class a {
  Function foo() {
     echo 'hello';
  }
}

class b  extends a {
    function b() {
      $this->foo();
    }
}


# Using Isntantiation
class a {
  Function foo() {
     echo 'hello';
  }
}

class b{
    var $a;
    function b() {
      $this->a = new a;
      $this->a->foo();
    }
}

#  Using non-instantiation:

class a {
  Function foo() {
     echo 'hello';
  }
}

class b{
    var $a;
    function b() {
      a::foo();
    }
}

Re: calling a class from within a class

Posted: Thu May 25, 2006 1:59 pm
by bdlang
The Ninja Space Goat wrote:

Code: Select all

class someClass($val, $classInsance){
    function someClass($val, $instance){
        $this->val = $val;
        $this->class = $insance;

        $this->class->method('bla bla');
    }
}
I'd just like to point out the fact that you're attempting to pass arguments to the 'class' statement. The constructor someClass() can of course take an argument, whether it's an INT type, a string or another Object as you're indicating. More appropriately:

Code: Select all

class someClass
{
   // declare our object variables (PHP4 style)
    var $val=null; // some value
    var $class=null; // the object we're aggregating

    /*
     * constructor takes two arguments,
     * a standard variable and another instantiated object
     * Note passing the object by reference, as mentioned (PHP4)
    */
    function someClass($val,  &$instance){ // PHP4 style
        $this->val = $val;

        // assign the object variable
        $this->class = $instance;
        // utiilize one of the aggregated object's methods
        $this->class->method('bla bla');
    }
}
While all the other examples are good (and mainly taken from here), I think there is lacking the rudimentary question: what is it exactly you're trying to accomplish?

Posted: Thu May 25, 2006 2:05 pm
by Flamie
Agreed, depending on what you need it for and the sizes (memory wise) of the classes theres multiple ways of doing it.
But to comment on your 1st question, it would be really suck to not be able to call a class within a class, because thats the whole point of OOP, its to have as less procedural code as possible ;)
For exemple for the game I'm making the only procedural code I have, is this:

Code: Select all

<?php
ob_start();
$id = $_COOKIE['rpg_id'];
$game = new Game($id);
$game->CheckRefresh();
$game->LoadPlayer();
$game->UpdatePlayer();
$game->CheckEvent();
$game->Render();
$buffer = ob_get_contents();
ob_end_clean();
echo $buffer;
?>
The rest is all being done thru the classes, game class creates a whole lot of instances of other classes in its functions etc.

So yeah, again it all depends what you need it for.

Posted: Thu May 25, 2006 2:15 pm
by Luke
might I suggest to a mod to merge this with the topic in PHP Theory and Design as it has morphed into basically the same conversation...

Re: calling a class from within a class

Posted: Thu May 25, 2006 2:34 pm
by Christopher
The Ninja Space Goat wrote:Can you do something like this? (I know this doesn't work because I tried it, but is it possible using correct syntax)?
Not only is it correct, composition has become the prefered way for object to use other objects -- over inheritence. The both have there uses, but old-school OO used inheritance for everything.