PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
sunegtheoverlord
Forum Commoner
Posts: 28 Joined: Thu Feb 18, 2010 5:02 am
Post
by sunegtheoverlord » Thu Jul 01, 2010 5:58 am
Hi guys and gals,
I'm following this tutorial on OOPHP
http://www.killerphp.com/tutorials/object-oriented-php/
how come i get an HTTP 500 Internal Server Error when trying to run this code
Code: Select all
<?php include("class_lib.php"); ?>
</head>
<body>
<?php
$stefan = new person();
$jimmy = new person;
$stefan->set_name("Stefan Mischook");
$jimmy->set_name("Nick Waddles");
echo "Stefan's full name: " . $stefan->get_name();
echo "Nick's full name: " . $jimmy->get_name();
?>
</body>
</html>
when i change it to this
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1" />
<title>OOP in PHP</title>
<?php include ("class_lib.php"); ?>
</head>
<body>
<?php
echo "hello";
?>
</body>
</html>
It displays "hello" fine.
What am i missing? I'm using 5.2.8.
Ta
S
internet-solution
Forum Contributor
Posts: 220 Joined: Thu May 27, 2010 6:27 am
Location: UK
Post
by internet-solution » Thu Jul 01, 2010 7:08 am
Is your error_reporting on?
If not, stick the following at top of your page and see what errors are displayed
Code: Select all
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
?>
Benjamin
Site Administrator
Posts: 6935 Joined: Sun May 19, 2002 10:24 pm
Post
by Benjamin » Thu Jul 01, 2010 8:11 am
I think the person constructor is calling the set_name method, which is then calling itself statically in a loop. Maybe you used = rather than == in the if/else conditional.
mikosiko
Forum Regular
Posts: 757 Joined: Wed Jan 13, 2010 7:22 pm
Post
by mikosiko » Thu Jul 01, 2010 8:27 am
the second part that you posted is working because you are not doing anything (other than include the file) with the class (or classes/functions) that you defined in your class_lib.php file.
Post the code that you have in your class_lib.php to try to identify your problem.
sunegtheoverlord
Forum Commoner
Posts: 28 Joined: Thu Feb 18, 2010 5:02 am
Post
by sunegtheoverlord » Thu Jul 01, 2010 8:49 am
Hello again,
Thanks for the replies, the code in class_lib.php is
Code: Select all
<?php
class person {
var $name;
function set_name($new_name) {
$this->name = $new_name;
}
function get_name() {
return $this->name;
}
}
?>
any more help?
sunegtheoverlord
Forum Commoner
Posts: 28 Joined: Thu Feb 18, 2010 5:02 am
Post
by sunegtheoverlord » Thu Jul 01, 2010 8:52 am
By the way i put on error reporting as suggested.
I get the following error
Notice: Undefined variable: stephan in /usr/home/toby/enquiries/htdocs/OO/index1.php on line 23
Fatal error: Call to a member function get_name() on a non-object in /usr/home/toby/enquiries/htdocs/OO/index1.php on line 23
sunegtheoverlord
Forum Commoner
Posts: 28 Joined: Thu Feb 18, 2010 5:02 am
Post
by sunegtheoverlord » Thu Jul 01, 2010 8:55 am
Sorry guys,
worked it out, i mispelled "Stefan" on line 21.....
"*£(!"£*($&*(&¬!
Thanks for all your time and help anyway.
S
Jade
Forum Regular
Posts: 908 Joined: Sun Dec 29, 2002 5:40 pm
Location: VA
Post
by Jade » Thu Jul 01, 2010 8:59 am
For some reason it doesn't think $stefan is a person object. I also noticed you're missing () behind the person class for your $jimmy variable.
Have you tried doing:
Code: Select all
<?php include("class_lib.php"); ?>
</head>
<body>
<?php
$stefan = new person();
$stefan->set_name("Stefan Mischook");
print_r($stefan); //print out the values of stefan and make sure it shows it as a [person] object with a [name] => Stefan Mischook
echo "Stefan's full name: " . $stefan->get_name();
?>
</body>
</html>