[HELP] Why No Display?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hide2may
    New Member
    • Jul 2009
    • 5

    [HELP] Why No Display?

    Hi there,

    I'm learning from PHP5 Power Programming by Gutmans, Bakken & Rethans and have come to p.58. The book says the output is:

    Judy
    Joe

    However I get nothing from neither IE8 nor FF3 instead!
    My machine is XP + PHP5 + Apache2.2. The error message is:

    Catchable fatal error: Object of class Person could not be converted to string in C:\Program Files\...\PHP5 PP\58.php on line 24

    Any clue?

    Thanks in advance,
    -hide2may

    // Here is the source:
    Code:
    <html>
    <title>PHP5 Power Programming</title>
    <head>P.58</head>
    <body>
    <?php
    
       error_reporting(E_ALL);
       ini_set('display_errors', true);
    
       class Person {
    
          function _construct($name) {
             $this->name = $name;
          }
    
          function getName() {
             return $this->name;
          }
    
          private $name;
       
       };
    
       $judy = new Person("Judy") . "\n";  // <---- this is line 24
       $joe = new Person("Joe") . "\n";
    
       print $judy->getName();
       print $joe->getName();
    
    ?>
    </body>
    </html>
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    the problem is
    Code:
    $judy = new Person("Judy")[U] . "\n"[/U];
    if you instantiate an object, you cannot apply any other operation at the same time.

    Code:
    // correct
    $judy = new Person("Judy");
    in your case the string concatenation forces new Person("Judy") to be converted to a string, which doesn’t work.

    guess it’s a printing mistake, should probably be like in the previous example (and this would actually make sense)
    Code:
    $judy = new Person("Judy"); 
    $joe = new Person("Joe"); 
    print $judy->getName() . "\n"; 
    print $joe->getName() . "\n";
    and one additional note
    Code:
    $x = new Sample;
    // is the same as
    $x = new Sample();
    Originally posted by hide2may
    However I get nothing from neither IE8 nor FF3 instead!
    since PHP runs on the server both browsers get the same output (unless you start working with HTTP/1.1* data (like Accept header…))

    * see RFC 2616

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      However, you can have a __toString() implementation that will allow you to do what you attempt above (although I don't think it's quite the right way to do it).

      Code:
      	class Person {
      	
      		private $person;
      		
      		public function __construct($name) {
      			$this->person = $name;
      		}
      		
      		public function __toString() {
      			return $this->person;
      		}
      	
      	}
      	
      	$person = new Person("mark") . " - hi";
      	
      	echo $person;

      Comment

      • hide2may
        New Member
        • Jul 2009
        • 5

        #4
        Oh, no, Dormilich! Even I changed the relevant code to the followings, still get no display! How come?

        ...
        $judy = new Person("Judy");
        $joe = new Person("Joe");

        print $judy->getName( );
        print $joe->getName( );

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          Originally posted by Markus
          … that will allow you to do what you attempt above.
          even book printers can make mistakes. I don’t think hide2may actually meant to do that.

          Comment

          • hide2may
            New Member
            • Jul 2009
            • 5

            #6
            No, Dormilich, I don't want to do Markus' way which is too far for me as a PHP beginner. My problem now becomes that even I changed those 4 lines mentioned by Dormilich in post 2, I still don't get the desired output, though the error message has gone. So, how do I proceed? Thanks again ^.^

            The 4 lines have already been changed to:

            $judy = new Person("Judy");
            $joe = new Person("Joe");

            print $judy->getName( ) . "\n";
            print $joe->getName( ) . "\n";

            Comment

            • hide2may
              New Member
              • Jul 2009
              • 5

              #7
              Ah... the same thing happens again in p.70!
              The book says the output is:

              In Ancestor constructor
              In Child constructor

              But in my machine: No desired display, no error message!
              I doubt that it has something to do with _construct( )
              any ideas, please?

              // Here is the code:
              Code:
              <html>
              <title>PHP5 Power Programming</title>
              <head>P.70</head>
              <body>
              <?php
              
                 error_reporting(E_ALL);
                 ini_set('display_errors', true);
              
                 class Ancestor {
                    const NAME = "Ancestor";
                    function _construct() {
                       print "In " . self::NAME . " constructor\n";
                    }
                 }
              
                 class Child extends Ancestor {
                    const NAME = "Child";
                    function _construct() {
                       parent::_construct();
                       print "In " . self::NAME . " constructor\n";
                    }
                 }
              
                 $obj = new Child();
              ?>
              </body>
              </html>

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                all the magical methods (__construct(), __destruct(), __get(), __set(), __call(), …) are written with 2 underscores in the beginning.

                for the correct syntax also refer to the manual

                PS: please use &#91;code] &#91;/code] tags when posting code

                Comment

                • Markus
                  Recognized Expert Expert
                  • Jun 2007
                  • 6092

                  #9
                  Easy mistake - magic methods use two preceding underscores.

                  Code:
                  public function __construct()
                  
                  // not!
                  public function _construct()

                  Comment

                  • Markus
                    Recognized Expert Expert
                    • Jun 2007
                    • 6092

                    #10
                    Whoops - beaten to it :P

                    Comment

                    • hide2may
                      New Member
                      • Jul 2009
                      • 5

                      #11
                      okay, that works!
                      thanks man \O/

                      Comment

                      Working...