Assigning a variable as default value for fn parameter......

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
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Assigning a variable as default value for fn parameter......

Post by raghavan20 »

I have a function for which I want to set the default value of the parameter to a class variable...but it gives error...is there is any alternative to it...

Code: Select all

function getFileSize($fileName = $this->fileName){
		if (file_exists($fileName)){
			return filesize($fileName);
		}else{
			trigger_error("File does not exist!!!", E_USER_WARNING);
			return FALSE;
		}
	}
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Assigning a variable as default value for fn parameter..

Post by Chris Corbyn »

raghavan20 wrote:I have a function for which I want to set the default value of the parameter to a class variable...but it gives error...is there is any alternative to it...

Code: Select all

function getFileSize($fileName = $this->fileName){
		if (file_exists($fileName)){
			return filesize($fileName);
		}else{
			trigger_error("File does not exist!!!", E_USER_WARNING);
			return FALSE;
		}
	}
This is the standard way to acheive it....

Code: Select all

<?php

function getFileSize($fileName = false){

	if ($fileName === false) $fileName = $this->fileName;

	if (file_exists($fileName)){
		return filesize($fileName);
	}else{
		trigger_error("File does not exist!!!", E_USER_WARNING);
		return FALSE;
	}
}

?>
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

This is a serious limitation is not it...there is no reason why you should not have this option enabled....is this the same with PHP 5?...is there going to be fix in PHP 6?
Post Reply