Showing posts with label PHP MAGIC CONSTANTS. Show all posts
Showing posts with label PHP MAGIC CONSTANTS. Show all posts

Friday, 5 October 2018

PHP - Magic constants

 Although these are not just symbols but important part of this token family. There are eight magical constants that change depending on where they are used.
__LINE__: The current line number of the file.
__FILE__: The full path and filename of the file. If used inside an include, the name of the included file is returned. Since PHP 4.0.2, __FILE__ always contains an absolute path with symlinks resolved whereas in older versions it contained relative path under some circumstances.
__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. (Added in PHP 5.3.0.)
__FUNCTION__: The function name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the function name as it was declared (case-sensitive). In PHP 4 its value is always lowercased.
__CLASS__: The class name. (Added in PHP 4.3.0) 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.
__TRAIT__: The trait name. (Added in PHP 5.4.0) 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).
__METHOD__: The class method name. (Added in PHP 5.0.0) The method name is returned as it was declared (case-sensitive).
__NAMESPACE__: The name of the current namespace (case-sensitive). This constant is defined in compile-time (Added in PHP 5.3.0).

Tuesday, 11 September 2018

PHP Magic Constants

There are several PHP "magic constants" (or "magical contants") which can be useful for a variety of reasons. These magic constants aren't actually constants at all, but effectively behave like them, although the values change depending on the context.
These are as follows.

__FILE__

__FILE__ is the full path and file name of the file that is being parsed. This can be useful for debugging purposes, and also for determining the absolute path of the current directory when used in conjunction with the dirname()function. For example, if the file that is echoing out __FILE__ is in /var/www/htdocs/example.php, the following would be output:
Example code:
echo __FILE__;

Example output:
/var/www/htdocs/example.php

Example code:
echo dirname(__FILE__);

Example output:
/var/www/htdocs
Note that if the file __FILE__ is used in is an include file, then the value of __FILE__ is the name of the include file, not the script that includes the file.

__DIR__

__DIR__ contains the directory of the file it is in. If the file is an include, it is the directory that include file is in. If it is the main script it is the directory that script is in.
__DIR__ is available from PHP 5.3; unless your application is designed to work only from PHP 5.3 or later, use the more traditional dirname(__FILE__) instead.

__LINE__

__LINE__ is the current line number of the file that is being parsed. This can be useful for debugging purposes.
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.

__CLASS__

Class is the class name. In PHP5 the value is case-sensitive and will be the exact case matched value of the class name; in PHP4 the value will be in lower case. As the following examples show, __CLASS__ is the class name of the class that it is called in; when calling a method from the parent's class, the parent's class is used.
class foo {
  function bar() {
    echo __CLASS__ . '<br />';
  }
}
class bar extends foo {
  function baz() {
    echo __CLASS__ . '<br />';
  }
}
foo::bar(); // echos 'foo'
bar::bar(); // echos 'foo'
bar::baz(); // echos 'bar'

__METHOD__

__METHOD__ is available from PHP 5.0, and is the name of the current method. It is returned as it was declared so is case sensitive.
class foo {
  function bar() {
    echo __METHOD__ . '<br />';
  }
}

// the example below echos foo::bar
foo::bar(); 

// the example below echos foo::bar
$foo = new foo();
$foo->bar();

__FUNCTION__

__FUNCTION__ is the fuction name of the current function, and works for both class methods and regular functions. In PHP5 the value is case-sensitive and will be the exact case matched value of the function name; in PHP4 the value will be in lower case.
The first example below shows using __FUNCTION__ in a class method; the second from a regular function.
class foo {
  function bar() {
    echo __FUNCTION__ . '<br />';
  }
}

// the example below echos bar
foo::bar();

function bar() {
  echo __FUNCTION__ . '<br />';
}

// the example below echos bar
bar();
The PHP manual reference page for magic constants is at http://www.php.net/manual/en/language.constants.predefined.php

Related posts:

Friday, 26 June 2015

PHP: PHP Magic Constants: Usage and Debugging with Examples

Constants play an important role for writing different PHP scripts. I believe, it is very good and professional approach to design any PHP script in a way that can easily be configured on different environments like Linux or Windows – and it can easily be achieved by defining useful constants in “config.php” or any other file depending on the script nature.

Thanks to PHP Magic Constants who have made my life easy for writing an easy to use config files in my free WordPress themes i.e Chip Zero, Chip Life, Chip Photo and  PHP classes i.e Chip File Download, Chip File Upload, Chip Password Generator and Chip CSV Parser.

Let’s learn about PHP Magic Constants one by one,
1. __LINE__

    The current line number of the file.

It is a constant of dynamic nature as it depends on the line that it’s used on in your script. It is very useful constant for debugging or any other purpose.

Example

PHP Magic Constants __LINE__

Preview 
Line Number: 11

So you can easily get information about the line number of PHP script in your file
2. __FILE__

    The full path and filename of the file. If used inside an include, the name of the included file is returned. Since PHP 4.0.2, __FILE__ always contains an absolute path with symlinks resolved whereas in older versions it contained relative path under some circumstances.

It is one of my favorite PHP Magic Constant, as i have used it several times in my different PHP scripts. It is very helpful to calculate path of your script file.


<?php
echo __FILE__;
?>

Preview
E:\wamp\www\tutorialchip\magic_constants.php

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. (Added in PHP 5.3.0.)

Great, It is really a powerful PHP Magic Constant. It resolves the headache of calculating directory paths as the entire PHP application depends on the correct path. Let’s have an idea,
 
<?php
echo __DIR__;
?>

Preview
E:\wamp\www\tutorialchip

As a PHP developer, is it not a great blessing ?

4. __FUNCTION__

    The function name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the function name as it was declared (case-sensitive). In PHP 4 its value is always lowercased.

This interesting PHP Magic Constant gives the function name.
   
<?php
/**
Sample Function
*/

function sayHello () {
    echo "Hello by " . __FUNCTION__ . " function.";
}

/**
Function Call
*/
sayHello();
?>

Preview
   
Hello by sayHello function.
5. __CLASS__

    The class name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the class name as it was declared (case-sensitive). In PHP 4 its value is always lowercased.

This PHP Magic Constant will output the class name.
   
<?php
/**
Sample Class
*/

class TutorialChip {

    /**
    Class Constructor
    */

    function __construct() {
        echo "I am constructor of " . __CLASS__ . " class.";
    }

}

/**
Class Object
*/

$object = new TutorialChip();

?>

Preview
   
I am constructor of TutorialChip class.
6. __METHOD__

    The class method name. (Added in PHP 5.0.0) The method name is returned as it was declared (case-sensitive).

This PHP Magic Constant will provide you information about class method. Let’s have a look,


   
<?php
/**
Sample Class
*/

class TutorialChip {

    /**
    Class Constructor
    */

    function __construct() {
        echo "I am constructor of " . __CLASS__ . " class.";
    }

    /**
    Say Hello by Class Method
    */

    function sayHello() {
        echo "<br />Hello by " . __METHOD__;
    }

}

/**
Class Object
*/
$object = new TutorialChip();
$object->sayHello();
?>

Preview
   
I am constructor of TutorialChip class.
Hello by TutorialChip::sayHello
7. __NAMESPACE__

    The name of the current namespace (case-sensitive). This constant is defined in compile-time (Added in PHP 5.3.0).


<?php
namespace TutorialChip;
/**
Namespace
*/
echo "Namespace name is " . __NAMESPACE__;
?>

Preview 
Namespace name is 'TutorialChip'.

Thursday, 4 September 2014

Constants in PHP

A constant is a name or an identifier for a simple value. A constant value cannot change during the execution of the script. By default a constant is case-sensitiv. By convention, constant identifiers are always uppercase. A constant name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. If you have defined a constant, it can never be changed or undefined.
To define a constant you have to use define() function and to retrieve the value of a constant, you have to simply specifying its name. Unlike with variables, you do not need to have a constant with a $. You can also use the function constant() to read a constant's value if you wish to obtain the constant's name dynamically.

constant() function:

As indicated by the name, this function will return the value of the constant.
This is useful when you want to retrieve value of a constant, but you do not know its name, i.e. It is stored in a variable or returned by a function.

constant() example:

<?php

define("MINSIZE", 50);

echo MINSIZE;
echo constant("MINSIZE"); // same thing as the previous line

?>
Only scalar data (boolean, integer, float and string) can be contained in constants.

Differences between constants and variables are:

  • There is no need to write a dollar sign ($) before a constant, where as in Variable one has to write a dollar sign.
  • Constants cannot be defined by simple assignment, they may only be defined using the define() function.
  • Constants may be defined and accessed anywhere without regard to variable scoping rules.
  • Once the Constants have been set, may not be redefined or undefined.

Valid and invalid constant names:

// Valid constant names
define("ONE",     "first thing");
define("TWO2",    "second thing");
define("THREE_3", "third thing")
// Invalid constant names
define("2TWO",    "second thing");
define("__THREE__", "third value"); 

PHP Magic constants:

PHP provides a large number of predefined constants to any script which it runs.
There are five 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. These special constants are case-insensitive and are as follows:
A few "magical" PHP constants ate given below:
NameDescription
__LINE__The current line number of the file.
__FILE__The full path and filename of the file. If used inside an include,the name of the included file is returned. Since PHP 4.0.2, __FILE__ always contains an absolute path whereas in older versions it contained relative path under some circumstances.
__FUNCTION__The function name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the function name as it was declared (case-sensitive). In PHP 4 its value is always lowercased.
__CLASS__The class name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the class name as it was declared (case-sensitive). In PHP 4 its value is always lowercased.
__METHOD__The class method name. (Added in PHP 5.0.0) The method name is returned as it was declared (case-sensitive).