I want to learn OOP, and I found an article online that shows some PHP OOP examples. When I copy them and attempt to run them, I get the following error:
I have the following two scripts, the first one titled index.php:
The second script is:
The way I understand it is the index.php script should call the Student.php class (second script) passing it two variables, and Student.php should print the values in a web page, but all I get is the error message. I'm new to OOP, so I may be totally off base by how this should be coded, I'm just going by the article I found on the web. Any help, or explanations will be greatly appreciated!
Code:
[COLOR=red][b]Fatal error: Cannot access empty property in /Users/peterv/Sites/PROJECT3/index.php on line 13[/b][/color]
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>OOP Test Project</title>
</head>
<body>
<?php
include "Student.php";
$a_student = new Student("John","Male");
print $a_student->$Name;
?>
</body>
</html>
Code:
<?php
class Student {
var $Name;
var $Gender;
/* Constructor */
function Student($studentName, $studentGender) {
$this->Name=$studentName;
$this->Gender=$studentGender;
}
}
?>
The way I understand it is the index.php script should call the Student.php class (second script) passing it two variables, and Student.php should print the values in a web page, but all I get is the error message. I'm new to OOP, so I may be totally off base by how this should be coded, I'm just going by the article I found on the web. Any help, or explanations will be greatly appreciated!