Jump to content

Archived

This topic is now archived and is closed to further replies.

areric

OO PHP, Calling functions in classes

Recommended Posts

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
[code]
<? 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();
?>
[/code]

ImageModifier.php
[code]
<? //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;
    }
  }
?>
[/code]

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

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]

[b]Any ideas?[/b]

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.

Share this post


Link to post
Share on other sites
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:
[code]$this->$imageModifier = new ImageModifier();
$this->$testHelpers = new TestHelpers();[/code]

RIGHT:
[code]$this->imageModifier = & new ImageModifier;
$this->testHelpers = & new TestHelpers;[/code]

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

Share this post


Link to post
Share on other sites
[!--quoteo(post=363513:date=Apr 10 2006, 08:33 PM:name=heckenschutze)--][div class=\'quotetop\']QUOTE(heckenschutze @ Apr 10 2006, 08:33 PM) [snapback]363513[/snapback][/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:
[code]$this->$imageModifier = new ImageModifier();
$this->$testHelpers = new TestHelpers();[/code]

RIGHT:
[code]$this->imageModifier = & new ImageModifier;
$this->testHelpers = & new TestHelpers;[/code]

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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.