0

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();
?>

4
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by macobex
0

File 1 containing class

/File 1
<?php
class foo{
	

}

?>

File 2

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

?>
0

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.

1

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.