Welcome to WebmasterWorld Guest from 54.167.155.147

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Can't instantiate a class with "defined"

Parse error

   
7:06 pm on Mar 6, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Code fragment:

// Get the class template
include ('inc/page.class.php');

// Page name
define('PLUME', $cleanfiletitle);
PLUME = new Page; // Parse error on this line

// For .txt filename
$filename = "' . $textfilename . '";

// If it exists...
if (file_exists($filename)) {
$file_contents = file_get_contents($filename);
$output = $file_contents;
} else {
$output = "No content. The associated text file could not be found.";
}

// Variables required by class template
PLUME->Textfilename = "' . $textfilename . '";
PLUME->SetContent($output);
PLUME->Template();

My class is Page. Everything works fine if I use a php variable $cleanfiletitle instead of PLUME. I would appreciate knowing why I get a parse error for the line indicated.

Patrick

7:32 pm on Mar 6, 2008 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



PLUME
in this case is a constant [php.net] and a constant is an identifier (name) for a simple value. As the name suggests, that value cannot change during the execution of the script.
7:33 pm on Mar 6, 2008 (gmt 0)

WebmasterWorld Senior Member whoisgregg is a WebmasterWorld Top Contributor of All Time 10+ Year Member



There are two reasons you get an error. First, constants cannot, by definition, be changed after they are initially set. Second, they can only be a scalar value (in other words, numbers, strings, and booleans are okay, but an object reference is not okay.)

Complete details about defining constants here [us3.php.net].

Added: coop beat me to it. :)

7:41 pm on Mar 6, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I see. Many thanks for both answers.

Patrick