Jump to content

OO PHP, Calling functions in classes


areric

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.
Link to comment
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 :)
Link to comment
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.