Help - Search - Members - Calendar
Full Version: PHP4 class question
Invision Power Services > Community Forums > Community Web Design and Coding
bılʞ
I'm having a few problems with classes today and I'm hoping someone here can help. Excuse my examples, they suck, I know.

Say, for example, I have 3 classes:

CODE
class base
{
   function base_function()
   {
       echo 'hello world!';
   }
}


CODE
class middle
{
   function middle_function()
   {
      echo 'hello there!';
    }
}


CODE
class extension extends base
{
   function func()
  {
      echo 'bye';
   }
}


"base" is my base class, and "extension" extends "base". "base" will also contain an instance of "middle":

CODE
$base = new base;
$base->middle = new middle;


However, I seem to be getting an error when I try to access "middle" and it's methods from within "extension":

CODE
class extension extends base
{
   function func()
   {
       $this->base_function()  
        $this->middle_function();
      echo 'bye';
   }
}


So this would be my code:

CODE
$base = new base;
$base->middle = new middle;

$extension = new extension;
$extension->func();


... and I expect this from the output:

QUOTE
hello world!
hello there!
bye!


But the error is:

QUOTE
Call to a member function on a non-object


(referring to the line containing $this->middle_function(); )

I've seen this error before obviously, but I'm not sure why I'm getting it. "middle" has been initialized through "base", and I can access methods etc in "base" okay, it just seems PHP doesn't like me touching objects that "base" has created.

Does that make sense? blink.gif
Rikki
There's two things to remember: classes and objects. A class is what you type in the file. An object is what you create FROM the class (with the new keyword).

When you extend a class, you're doing just that - extending the class, not any objects you've created from it. So, you can't access 'middle' from the 'extension' class because it's got nothing to do with the base class. Sure you've assigned it to a object property after you've made an object out of base, but it's not part of the class at any stage original.gif
bılʞ
Ahhhhhh. So there is no way around this? sad.gif

It's not important, I was just playing about after reading something earlier.
ZuCruTrooper2
First problem:

When calling $this->base_function, you forgot the ;, which will cause a problem on the next line.

Second, when you're extending a class, call the function that extended it, not the base. Thirdly, when you made a subclass out of another class, you have to go through that class to access the functions inside it. You tried to use $base->middle = new middle; and then $this->middle_function();. You needed to have used $extension->middle = new middle; and $this->middle->middle_function(); to call the functions. Understand? I'll try and reword if needed, sorry.

This works:

CODE
<?


class base
{
   function base_function()
   {
       echo 'hello world!';
   }
}

class middle
{
   function middle_function()
   {
      echo 'hello there!';
    }
}

class extension extends base
{
   function func()
   {
       $this->base_function();
       $this->middle->middle_function();
      echo 'bye';
   }
}


$extension = new extension;
$extension->middle = new middle;
$extension->func();
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.