Page 1 of 1

Dynamic Optional Parameter :)

Posted: Mon Feb 20, 2006 12:15 am
by quocbao
This looks funny :D

test.php

Code: Select all

<?

function f($f = __FILE__ , $l = __LINE__)
{
	echo $f . ":" . $l . "<BR>";
}

?>
test2.php

Code: Select all

<?

include("test.php");

f();
f(__FILE__,__LINE__);

?>
this may output something like

Code: Select all

xxxxxxxxx\test.php:3
xxxxxxxxx\test2.php:6
Then PHP "prepares" our parameter before we call it or after we call it ( funny )

Posted: Mon Feb 20, 2006 12:19 am
by feyd
what?

Posted: Mon Feb 20, 2006 2:21 am
by Chris Corbyn
Yes... those are constants, they are not dynamic, just the same as you can't do "5 = $foo;"

I'd love to know how you determine the line number of function was called from though... it would be massively useful in debugging.

backtrace() can help by the way if that's where you're heading ;)

Posted: Mon Feb 20, 2006 3:54 am
by quocbao
feyd wrote:what?
Well , this is a small script , i just want to get line number and file which this function is called from .
Of couse , using Debug backtrace will make life easier :P

I tried this

Code: Select all

<?

echo __LINE__; //3
echo __LINE__; //4


?>
so i think when we call this function

Code: Select all

function f($l = __LINE__,$f = __FILE__)
{
//want to do something here 
}
$l and $f will become the line and filename where it is called , but i was wrong :oops: (as you can see)

Posted: Mon Feb 20, 2006 4:33 am
by Chris Corbyn
quocbao wrote:
feyd wrote:what?
Well , this is a small script , i just want to get line number and file which this function is called from .
Of couse , using Debug backtrace will make life easier :P

I tried this

Code: Select all

<?

echo __LINE__; //3
echo __LINE__; //4


?>
so i think when we call this function

Code: Select all

function f($l = __LINE__,$f = __FILE__)
{
//want to do something here 
}
$l and $f will become the line and filename where it is called , but i was wrong :oops: (as you can see)
Yep, because where __LINE__ and __FILE__ are parsed they will just the the same as the info for the part of the file you put them at ;) They don't change when they get used, they are fixed values :)

Posted: Mon Feb 20, 2006 9:58 am
by feyd
debug_backtrace() can be used for this.