I'm a journeyman Perl programmer but new to PHP. My problem seems to be with including or requiring a class. In my php.ini file I have the include path set to either the current directory or the php/includes directory right off the root. ie. ".:/php/includes". I have no problem including a "regular" php file but when I include a class file I get the "unable to instantiate non existent classs" error. I have tried placing the class file in both the current directory and in the php/includes directory to no avail. I noticed that the source code of the html page containing the error does indeed show the php code in the class file. I've read every tutorial and manual entry I can find but I'm still unsure about things such as naming conventions for the class file. I've seen examples where the file name starts with "cls" (clsFileName.inc) and has a .inc suffix. I've also seen files with .php extensions. I have named my test class file Test.php with the class name of Test with a constructor method of Test. I have also tried naming the file Test.inc. I've tried including and requiring the file. I know I'm missing something trivial but after 4 days of reading and just trying different crap, I'm stumped. I think I'll like PHP, maybe even more than Perl if I can get a handle on the basics. Thanks in advance for any help.
can't instantiate new class instance
can we see some code?
Hi Perlmonger,
I am not familier with classes. BUT I do know that PhP first includes the code in the individually included files, and after that, evaluates the code. So the including in itself SHOULD not give you any problem. It may be more likely that the class itself gives you the headache.
J.
but I'm still unsure about things such as naming conventions for the class file.
There arent any enforced naming conventions for filenames.
In fact, you could even treat .html files as PHP files, if you configured the webserver to do so.
I have also tried naming the file Test.inc.
Unless your webserver is configured to treat .inc files as PHP files, that usually isnt a good idea.
Just stick to .php, or perhaps to .inc.php or .class.php (since then the extension is still actually .php)
Let's do a test with 2 files, test.php and class.php, which we shall place in the same directory to simplify matters:
<?php
//test.php
require 'class.php';
if (class_exists("Foo")) {
$bar = new Foo("Hello World!");
echo $bar->announce();
} else {
echo "Class 'Foo' does not exist!";
}
?>
<?php
//class.php
class Foo {
var $announcement;
function Foo($a = '') {
$this->announcement = $a;
}
function announce() {
return $this->announcement;
}
}
?>
Run test.php and see what you get.
Thank you for the replies. Laserlight: I am at work now but I will try your suggestion when I return home and post the results. Thanks again!