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: - <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>
10 1871
the problem is - $judy = new Person("Judy") . "\n";
if you instantiate an object, you cannot apply any other operation at the same time. - // 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) - $judy = new Person("Judy");
-
$joe = new Person("Joe");
-
print $judy->getName() . "\n";
-
print $joe->getName() . "\n";
and one additional note - $x = new Sample;
-
// is the same as
-
$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
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). -
class Person {
-
-
private $person;
-
-
public function __construct($name) {
-
$this->person = $name;
-
}
-
-
public function __toString() {
-
return $this->person;
-
}
-
-
}
-
-
$person = new Person("mark") . " - hi";
-
-
echo $person;
-
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( );
@Markus
even book printers can make mistakes. I don’t think hide2may actually meant to do that.
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";
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: - <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>
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
Easy mistake - magic methods use two preceding underscores. -
public function __construct()
-
-
// not!
-
public function _construct()
-
okay, that works!
thanks man \O/
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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 `'}''...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
| |