calling a class from 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
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

calling a class from within a class

Post 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...
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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)
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post 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();
    }
}
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Re: calling a class from within a class

Post 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?
Last edited by bdlang on Thu May 25, 2006 2:19 pm, edited 1 time in total.
Flamie
Forum Contributor
Posts: 166
Joined: Mon Mar 01, 2004 3:19 pm

Post 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.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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...
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: calling a class from within a class

Post 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.
(#10850)
Post Reply