Jump to content


Photo

Fatal error: Call to a member function


  • Please log in to reply
1 reply to this topic

#1 ScrollMaster

ScrollMaster
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 15 May 2006 - 08:00 PM

help for I am a exterme php newb and I need some freaky php freak to come and pwn me with the anwser to my problem.

Here is my code:

<?php
#================================================================
  class FlyBehaviour
  {

    public function Fly()
    {
      print "<p>"."Duck is Flying!, Almost!"."</p>"."\n";
    }
  }
#================================================================
  class QuackBehaviour
  {
    public function Quack()
    {
      print "<p>"."Duck is Quacking!, Almost!"."</p>"."\n";
    }
  }
#================================================================
  class Duck
  {
     # Variables ------------------------------------------------
     public $fly;
     public $quack;
     # Functions ------------------------------------------------
     public function __construct()
     {

         print "<p>"."Created a Duck!"."</p>"."\n";
     }
     public function performFly()
     {
       $fly->Fly();
     }
     public function performQuack()
     {
       $quack->Quack();
     }

  }
#================================================================
  $duck = new Duck();
  $duck->performFly();
  $duck->performQuack();
?>

Output:

Created a Duck!

Fatal error: Call to a member function Fly() on a non-object in E:\Program\www\localhost\simUduck\index.php on line 33


and Above is my error...

What have I done wrong? What rule have I missed? What Syntax have I wrong?
Please Help!

#2 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 15 May 2006 - 08:17 PM

in the class Duck, you have to make $fly an instance of FlyBehavior before it knows how to use Fly().

public function __construct()
     {
         $this->fly = new FlyBehavior;
         print "<p>"."Created a Duck!"."</p>"."\n";
     }

Also, I believe you have to change $fly->Fly() with $this->fly->Fly().
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users