Page 1 of 1

Not returning properly

Posted: Thu Jun 02, 2005 7:28 pm
by SBro
Hi guys, I have this seemingly silly problem. I'm not sure whether it's to do with php5 or not, as I've done this type of thing numerous times before without any problems, what stupid thing am I missing?

Code: Select all

class files {
	
	/**
	 * @return array
	 * @param mixed $file
	 * @desc class constructer, places contents of file in array
	 **/
	function files($file) {
		return file($file);
	}
	
}
I'm calling this class on a seperate page as follows:

Code: Select all

require_once('common/files.class.php');

$f = new files($_FILES['file']['tmp_name']);

var_dump($f);
The problem being the results of var_dump are

Code: Select all

object(files)#1 (0) { }
This is not correct as doing a var_dump within the function files() confirms everything is as it should be, just not after it is returned. Any ideas what i'm doing wrong? Also as I've just upgraded to php5 should I be able to use the _construct() instead of the function files() in my class ? thanks.

Posted: Thu Jun 02, 2005 7:53 pm
by Ambush Commander
This is not correct as doing a var_dump within the function files() confirms everything is as it should be, just not after it is returned.
I suspect something changed in how PHP5 handles object initialization. They rewrote the object model, so you might have been affected. Just a guess, though. My recommendation is to create a factory function.
Also as I've just upgraded to php5 should I be able to use the _construct() instead of the function files() in my class ?
It's __construct() (with two _s), but yeah, that's right. However, for backwards compatibility, if PHP doesn't find __construct(), it will look for the PHP4esque name_of_class_function() as a constructor.

Posted: Thu Jun 02, 2005 7:57 pm
by SBro
Yes silly me, it was the __ that was the problem, I was only using one _ :roll: Thanks for your help.

Posted: Fri Jun 03, 2005 1:12 am
by Syranide
eitherway, the constructor REALLY shouldn't return anything... as then why do you have the class with a constructor... that becomes a string when created...

what you want I'm guessing is a static function (to package functions into classes for readability) .. then calling e.g. file::load(); ... or just use a common function.

the way you did it is NOT a good way to do it and may very well change.