Jump to content


Photo

OO PHP, Calling functions in classes


  • Please log in to reply
2 replies to this topic

#1 areric

areric
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 11 April 2006 - 12:04 AM

Ok all im having a few problems and for the life of me cant figure this out. Im trying some object oriented php for the first time. Specifically a image manipulation library. At the same time im trying to write it in a test driven fashion. Im calling the function in my class from a function in my test class and getting an error. The weird thing is i have a third class for test utilities and i use that the SAME way and it works. THat said heres the code:

ImageModifierTest.php
<? include("TestLib.php"); ?>
<? include("ImageModifier.php"); ?>
<?
  class ImageModifierTest //implements ITest
  {
    var $imageModifier;
    function __construct()
    {
    }

    function TestOpenBinaryStream()
    {
      $streamToSend = "111010001010001";
      $this->$imageModifier = new ImageModifier();
      $this->$testHelpers = new TestHelpers();
      return $this->$imageModifier->OpenBinaryStream($streamToSend);
      //return $this->$testHelpers->AssertAreEqual($binaryStream, $streamToSend);
    }
    function ExecuteTests()
    {
      $success = $this->TestOpenBinaryStream();
      if ($success == true)
      {
        return "Success";
      }
      else
      {
        return "Failure";
      }
    }
  }
?>

<?
  $imageModifierTests = new ImageModifierTest();
  echo $imageModifierTests->ExecuteTests();
?>

ImageModifier.php
<? //include("ITest.php"); ?>
<?
  class ImageModifier
  {
    //MEMBERS
    var $mImage = "";
    var $mModifiedImage = "";
    var $mBinaryImageStream = "";

    function __construct()
    {
    }

    function SetImageFile($image)
    {
        $mImage = $image;
    }

    function OpenBinaryStream($stream)
    {
      $this->$mImage = $stream;
      $this->$mBinaryImageStream = $stream;
      return $this->$mImage;
    }
  }
?>

TestLib.php
<? //TEST Helpers
class TestHelpers
{
  function AssertAreEqual($lhs, $rhs)
  {
    return $lhs == $rhs;
  }
}
?>

You might notice i have some commented out code in there. I was going to write this with interfaces and a few other features but then found out they are php5 only and im using php4 on my server.

The error message i get is :
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
Fatal error: Call to undefined function: openbinarystream() in /home/flyerweb/public_html/imagelibrary/ImageModifierTest.php on line 16
[/quote]

Any ideas?

Thanks

Note: Seeing how i commented out the AssertEquals above i should let you all know when that is uncommented it works fine. But the one above it doesnt.

#2 heckenschutze

heckenschutze
  • Members
  • PipPipPip
  • Advanced Member
  • 257 posts
  • LocationAustralia

Posted 11 April 2006 - 12:33 AM

When your pointing to variables in your class, you shouldn't be including the $ (only for $this->x), since your not initializing the classes with any values, we don't need to call them as "functions". You can also save resources, by creating a reference to the class.

WRONG:
$this->$imageModifier = new ImageModifier();
$this->$testHelpers = new TestHelpers();

RIGHT:
$this->imageModifier = & new ImageModifier;
$this->testHelpers = & new TestHelpers;

Fix up the rest of your code and give it a try :)

#3 areric

areric
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 11 April 2006 - 03:38 AM

[!--quoteo(post=363513:date=Apr 10 2006, 08:33 PM:name=heckenschutze)--][div class=\'quotetop\']QUOTE(heckenschutze @ Apr 10 2006, 08:33 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
When your pointing to variables in your class, you shouldn't be including the $ (only for $this->x), since your not initializing the classes with any values, we don't need to call them as "functions". You can also save resources, by creating a reference to the class.

WRONG:
$this->$imageModifier = new ImageModifier();
$this->$testHelpers = new TestHelpers();

RIGHT:
$this->imageModifier = & new ImageModifier;
$this->testHelpers = & new TestHelpers;

Fix up the rest of your code and give it a try :)
[/quote]


Awesome thanks heckenschutze, you actually answered two questions for the price of one :) I was trying to figure out how to do references.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users