Hey I'm a php beginner and I'm having some problems with the website I'm testing with. I'll show you first the relevant code.

index.php:

<?php
include "stdlib.php";
$page = new Page();
$page->title = "Index";
$page->printHeader();
?>
<style type="text/css">
div.container
{
	width:100%;
	margin:0px;
	border:1px solid gray;
}
div.left
{
	width:100px;
	float:left;
	padding:1em;
	margin:0;
}
div.content
{
	margin-left:190px;
	border-left:1px solid gray;
	padding:1em;
}
</style>
<div class="container">
<div class="left"><p class="left">Seth Baur</p></div>
<div class="content">
<p>Welcome to my site.</p>
<?php echo date("d-m-Y"); ?>
<form action="form.php" method="post">
Name: <input type="text" name="name" /><br />
Age: <input type="text" name="age" /><br />
<input type="submit" />
</form>
</div>
</div>
<?php
$page->printFooter();
?>

stdlib.php:

<?php
function __autoload($class_name){
	require_once $class_name . '.php';
}
?>

Page.php:

<?php
class Page {
	var $title;
	function printHeader() {
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
	<link rel="stylesheet" type="text/css" href="styles.css" />
	<title><?php echo $this->title; ?></title>
</head>
<body>
<?php
}
function printFooter() {
?>
</body>
</html>
<?php
}
}
?>

The error I'm getting now that I've put in the autoload function is:

Fatal error: Cannot instantiate non-existent class: page in /home/content/t/h/r/threepio/html/seth/php/index.php on line 3

All of these files are in the same directory, so I'm not sure why it can't find the Page class. Anyone have an idea?

Recommended Answers

All 3 Replies

Where is the variable $class_name coming from and how is it populated? On line 3 of index.php, you have $page = new Page(); but, you never defined the class Page() in stdlib.php. I see you did it on Page.php but this will only have a local scope of the page its defined in.

For interests sake, what happens when you replace:
include"stdlib.php";
with
include "Page.php"; ?

Try transforming Page into lowercase as well..

buddylee17:
The variable $class_name is passed to the autoload function when the it comes to a class that is not defined. At that point it includes the class it couldn't find with ".php" appended on the end.

MickRip:
And yes I could replace stdlib.php with Page.php, but the idea is that this way, I could include many classes, in individual files, without "including" each.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.