Page 1 of 1

Internal class functions don't seem to get executed.

Posted: Thu Oct 22, 2009 3:56 am
by bacoms
Hi. Can someone please put me out of my misery on this. I don't write much PHP and this has me baffled. I've probably work myself into the wrong mindset with the time I've spent on this. What's happening is that I've written a class containing some functions that work fine until I try to call other functions from within them. These functions don't seem to get executed.

The following example demonstrates this:

pagetest.php

Code: Select all

 
<html>
  <head>
    <title>Using Classes in PHP</title>
    <META NAME="keywords" CONTENT="PHP, Classes">
  </head>
  <body>
    <?php
      include "Page.class";
      $Sample = new Page;
      $Content = "<P>This page was generated by the Page Class example.</P>";
      $Sample->SetContent($Content);
      $Sample->Display1("Test1: ");
      #$Sample->Display2("Test2: ");
      #$Sample->Display3("Test3: ");
    ?>
  </body>
</html>
 


Page.class

Code: Select all

 
<?php
class Page 
  {
  var $Content;
  function Display1($text) 
    {
    echo "Display1 - " . $text . $this->Content . "\n";
    }
  function Display2($text) 
    {
    echo "Display2 - " . _getContent($text) . "\n";;
    }
  function Display3($text) 
    {
    _showContent($text);
    }
  function SetContent($text)
    {
    $this->Content = $text;
    }
  function _getContent($text)
    {
    return $text . $this->Content;
    }
  function _showContent($text)
    {
    echo "_showContent - " . $text . $this->Content . "\n";
    }    
  }
?>
 
Executing pagetest.php in a browser, with Page.class as shown, works fine and produces the following HTML:
  • <html>
    <head>
    <title>Using Classes in PHP</title>
    <META NAME="keywords" CONTENT="PHP, Classes">
    </head>
    <body>

    Display1 - Test1: <P>This page was generated by the Page Class example.</P>
    </body>

    </html>
However, if either or both of the lines
#$Sample->Display2("Test2: ");
#$Sample->Display3("Test3: ");
is/are uncommented and the script is rerun, the following is produced:
  • <html>
    <head>
    <title>Using Classes in PHP</title>
    <META NAME="keywords" CONTENT="PHP, Classes">
    </head>
    <body>

    Display1 - Test1: <P>This page was generated by the Page Class example.</P>
indicating that the script has terminated prematurely.

Is this a limitation of PHP or have I made some stupid mistake (I suspect the latter)?

Re: Internal class functions don't seem to get executed.

Posted: Thu Oct 22, 2009 4:49 am
by Mark Baker

Code: Select all

 
  function Display2($text) 
    {
    echo "Display2 - " . $this->_getContent($text) . "\n";;
    }
  function Display3($text) 
    {
    $this->_showContent($text);
    }
 

Re: Internal class functions don't seem to get executed.

Posted: Thu Oct 22, 2009 8:23 am
by bacoms
I knew it would be something stupid. Many thanks for taking the trouble of replying.