Not returning properly

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
SBro
Forum Commoner
Posts: 98
Joined: Tue Sep 30, 2003 10:06 pm

Not returning properly

Post 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.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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.
SBro
Forum Commoner
Posts: 98
Joined: Tue Sep 30, 2003 10:06 pm

Post by SBro »

Yes silly me, it was the __ that was the problem, I was only using one _ :roll: Thanks for your help.
Syranide
Forum Contributor
Posts: 281
Joined: Fri May 20, 2005 3:16 pm
Location: Sweden

Post 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.
Post Reply