What are the best practices for usage of PHP’s Magic Constants, such as __LINE__ , __FILE__, __FUNCTION__, __CLASS__ and __METHOD__?
For example I use __LINE__ and __CLASS__ to locate the SQL Error like this :
$result = mysql_query($query)
or die("Error SQL line ".__LINE__ ." class ".__CLASS__." : ".mysql_error());
Is this an acceptable practice?
The practice you show has two shortcomings:
You are not showing the file the error occurs in - you'd have to have a very strict file structure that maps classes to files 1:1 for this to be useful
__CLASS__will show you the wrong result if the error occurs in an inherited class. Useget_class($this)to get the actual current class.Other than that, using these constants is perfectly acceptable. Note that
__FUNCTION__and__CLASS__were added in PHP 4.3__METHOD__is available since PHP 5.0.0__DIR__and__NAMESPACE__are available since PHP 5.3.docs