473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

[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:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <title>PHP5 Power Programming</title>
  3. <head>P.58</head>
  4. <body>
  5. <?php
  6.  
  7.    error_reporting(E_ALL);
  8.    ini_set('display_errors', true);
  9.  
  10.    class Person {
  11.  
  12.       function _construct($name) {
  13.          $this->name = $name;
  14.       }
  15.  
  16.       function getName() {
  17.          return $this->name;
  18.       }
  19.  
  20.       private $name;
  21.  
  22.    };
  23.  
  24.    $judy = new Person("Judy") . "\n";  // <---- this is line 24
  25.    $joe = new Person("Joe") . "\n";
  26.  
  27.    print $judy->getName();
  28.    print $joe->getName();
  29.  
  30. ?>
  31. </body>
  32. </html>
Jul 26 '09 #1
10 1871
Dormilich
8,658 Expert Mod 8TB
the problem is
Expand|Select|Wrap|Line Numbers
  1. $judy = new Person("Judy") . "\n";
if you instantiate an object, you cannot apply any other operation at the same time.

Expand|Select|Wrap|Line Numbers
  1. // correct
  2. $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)
Expand|Select|Wrap|Line Numbers
  1. $judy = new Person("Judy"); 
  2. $joe = new Person("Joe"); 
  3. print $judy->getName() . "\n"; 
  4. print $joe->getName() . "\n"; 
and one additional note
Expand|Select|Wrap|Line Numbers
  1. $x = new Sample;
  2. // is the same as
  3. $x = new Sample();
@hide2may
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
Jul 26 '09 #2
Markus
6,050 Expert 4TB
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).

Expand|Select|Wrap|Line Numbers
  1.     class Person {
  2.  
  3.         private $person;
  4.  
  5.         public function __construct($name) {
  6.             $this->person = $name;
  7.         }
  8.  
  9.         public function __toString() {
  10.             return $this->person;
  11.         }
  12.  
  13.     }
  14.  
  15.     $person = new Person("mark") . " - hi";
  16.  
  17.     echo $person;
  18.  
Jul 26 '09 #3
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( );
Jul 26 '09 #4
Dormilich
8,658 Expert Mod 8TB
@Markus
even book printers can make mistakes. I don’t think hide2may actually meant to do that.
Jul 26 '09 #5
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";
Jul 26 '09 #6
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:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <title>PHP5 Power Programming</title>
  3. <head>P.70</head>
  4. <body>
  5. <?php
  6.  
  7.    error_reporting(E_ALL);
  8.    ini_set('display_errors', true);
  9.  
  10.    class Ancestor {
  11.       const NAME = "Ancestor";
  12.       function _construct() {
  13.          print "In " . self::NAME . " constructor\n";
  14.       }
  15.    }
  16.  
  17.    class Child extends Ancestor {
  18.       const NAME = "Child";
  19.       function _construct() {
  20.          parent::_construct();
  21.          print "In " . self::NAME . " constructor\n";
  22.       }
  23.    }
  24.  
  25.    $obj = new Child();
  26. ?>
  27. </body>
  28. </html>
Jul 26 '09 #7
Dormilich
8,658 Expert Mod 8TB
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 [code] [/code] tags when posting code
Jul 26 '09 #8
Markus
6,050 Expert 4TB
Easy mistake - magic methods use two preceding underscores.

Expand|Select|Wrap|Line Numbers
  1. public function __construct()
  2.  
  3. // not!
  4. public function _construct()
  5.  
Jul 26 '09 #9
Markus
6,050 Expert 4TB
Whoops - beaten to it :P
Jul 26 '09 #10
okay, that works!
thanks man \O/
Jul 26 '09 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: FrankBooth | last post by:
Hello, I have a list of names, and when I click ona name I want the extar info to show and then I want to clcik and hide it again. I have the following HTML which works perfectly if I use one...
8
by: Johno | last post by:
I have written the two associated base classes below (Digital_camera and Review) to manage digital camera and review objects. They are base classes for which other derived classes can be written to...
5
by: Zambien | last post by:
Hi all, Here's my problem. I have tables that are using the menu/submenu idea for hiding rows. This works fine in IE (of course) and does show/hide correctly in netscape, but as soon as the...
4
by: Joseph | last post by:
The idea is to show only one of the <Baby_Div> while hiding all the others. At the moment all I have managed to do is to show each <Baby_Div> in turn as expected, but the problem is that once a...
4
by: dixie | last post by:
Help, I'm really out of my depth here (not unusual I hear you say :-). I have just installed HTML Help in an application. I told it in the Project Properties the path to the help file. I then...
8
by: drose0927 | last post by:
Please help! I can't get my program to exit if the user hits the Escape button: When I tried exit(EXIT_SUCCESS), it wouldn't compile and gave me this error: Parse Error, expecting `'}''...
15
by: carr4895 | last post by:
Hello. I was wondering if someone could help me too with a login form. Upon startup, I have to display a password screen and it should accept a user name and password. User name can be anything...
7
by: arupfrancis | last post by:
Hi, I am trying to create a colored box using background images. I am able to do it easily using tables but doing it in divs is proving to be an issue. Moreover IE and Mozilla also seems to be...
2
by: gcook | last post by:
Hello, I need some help! I need to genreate a set of test inputs and expected results for the currency conversion program below. I also have to use the folowing format Test Case <put number...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.