Page 1 of 1

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

Posted: Mon Jan 30, 2006 4:37 pm
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;
		}
	}

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

Posted: Mon Jan 30, 2006 4:46 pm
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;
	}
}

?>

Posted: Mon Jan 30, 2006 5:56 pm
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?