Hi

can anyone help me out here, I did a class working fine but i can't make objects out of it. do i need to include the file that has the class in my new file? with an include statement?

say i have this.
#file 1
<?php

class foo
{
....
}
?>

#file 2
<?php

$obj = new foo();//is this valid?
/*

or should i use it like this first
/*

#include "file1.php"
$obj = new foo();
?>

Recommended Answers

All 3 Replies

File 1 containing class

/File 1
<?php
class foo{
	

}

?>

File 2

//File 2
<?php
include_once("location_of_file1");
$obj = new foo();

?>

do i need to include the file that has the class in my new file?

Yes, of course. Otherwise file 2 won't know what new foo() means. You have to provide the implementation of foo.

with an include statement?

You can use either include OR require. I prefer to use require since it will halt execution if I provide the wrong path to the file and will force me to figure out what is the correct path before continuing.

If you are working with a large project, it is much advisable to use the magic method __autoload() to include class files, rather than including them manually.
Including class files manually is frustrating especially if working with large set of classes, so i advice you to learn using __autoload().

http://php.net/manual/en/language.oop5.autoload.php

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.