Sunday 2 June 2013
Facebook StumbleUpon Twitter Google+ Pin It

PHP Constants and PHP Magic Constants

PHP constants

Variables offer a flexible way of storing data because you can change their values and the type of data they store at any time. If, however, you want to work with a value that you do not want to alter throughout your script’s execution, you can define a constant.
A constant is an identifier (name) for a simple value. As the name suggests, that value cannot change during the execution of the script (except for magic constants, which aren’t actually constants). A constant is case-sensitive by default. By convention, constant identifiers are always uppercase. The name of a constant follows the same rules as any label in PHP. A valid constant name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.
You can define a constant by using the define() – function or by using the const keyword outside a class definition as of PHP 5.3.0. Once a constant is defined, it can never be changed or undefined.
To use the define() function, you must place the name of the constant and the value you want to give it within the call’s parentheses. These values must be separated by a comma, like so:
1
2
3
4
5
6
7
8
9
<?php
define ("PI", 3.14);
define ("CONSTANT", "Hello World.");
print PI."<br>";
print CONSTANT;
?>
As of PHP 5, you can define constants within classes. You declare a constant using the const keyword at the top of your class. Constants are access by the class rather than an object as shown in below:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
class testClass {
     
    const PI = 3.14;
    const CONSTANT = 'Hello World.';
     
    function testFunc() {
        // some codes
    }
}
print testClass::PI."<br>";
print testClass::CONSTANT;
?>
You will get the same output for the above two php scripts as shown in below:
3.14
Hello World.

PHP Magic Constants

PHP provides a large number of predefined constants to any script which it runs. Many of these constants, however, are created by various extensions, and will only be present when those extensions are available, either via dynamic loading or because they have been compiled in.
There are eight magical constants that change depending on where they are used. For example, the value of __LINE__ depends on the line that it’s used on in your script.
Magic constants include: __LINE__, __FILE__, __DIR__, __FUNCTION__, __CLASS__, and __METHOD__. These special constants are case-insensitive. Magic constants mostly used for debugging purpose.
1. __LINE__
__LINE__ is the current line number of the file that is being parsed. This can be useful for debugging purposes. With this you can easily get details about the line numbers in files.
Note that if the file __LINE__ is used in is an include file, then the value of __LINE__ is the line number of the include file, not the script that includes the file.
2. __FILE__
Display the full path and filename of the file. If used inside an include, the name of the included file is returned. __FILE__ always contains an absolute path with symlinks resolved whereas in older versions it contained relative path under some circumstances.
3. __DIR__
The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to dirname(__FILE__). This directory name does not have a trailing slash unless it is the root directory.
4. __FUNCTION__
The function name. As of PHP 5 this constant returns the function name as it was declared (case-sensitive). In PHP 4 its value is always lowercased.
Check the first four magic constants with the below example. Notice that the parsing file used below is named index.php which is located on folder named ‘test’ inside WWW directory of the WAMP server.
1
2
3
4
5
6
7
8
9
10
11
12
<?php
     
function testFunction() {
    echo __LINE__."<br>";
    echo __FILE__."<br>";
    echo dirname(__FILE__)."<br>";
    echo __DIR__."<br>";
    echo __FUNCTION__."<br>";
}
testFunction();
?>
The output of the above php script is,
4
C:\wamp\www\test\index.php
C:\wamp\www\test
C:\wamp\www\test
testFunction
5. __CLASS__
The class name. This is one of the interesting Magic constant because it returns class name as a result. As of PHP 5 this constant returns the class name as it was declared (case-sensitive). In PHP 4 its value is always lowercased. The class name includes the namespace it was declared in (e.g. Foo\Bar). Note that as of PHP 5.4 __CLASS__ works also in traits. When used in a trait method, __CLASS__ is the name of the class the trait is used in.
6. __METHOD__
The class method name. Display the method name of class. One of the powerful Magic constant because of the method name is returned as it was declared (case-sensitive).
Check the __CLASS__ and __METHOD__ magic constants with the below example.
1
2
3
4
5
6
7
8
9
10
11
12
<?php
class testClass {
    function testFunction() {
        print __CLASS__."<br>";
        print __METHOD__."<br>";
    }
}
$testobj = new testClass();
$testobj->testFunction();
?>
The output of the above php code returns the class and method names as shown below,
testClass
testClass::testFunction
7. __TRAIT__
The trait name. As of PHP 5.4 this constant returns the trait as it was declared (case-sensitive). The trait name includes the namespace it was declared in (e.g. Foo\Bar).
8. __NAMESPACE__
The name of the current namespace (case-sensitive). This constant is defined in compile-time.
-By Parthiv Patel
Parthiv Patel
Bhaishri Info Solution
Sr. PHP Developer
Limdi Chowk, AT PO. Nar, Di. Anand
Nar, Gujarat
388150
India
pparthiv2412@gmail.com
7383343029
DOB: 12/24/1986

No comments: