Detailed Explanation of php Magic Variable Usage Example

  • 2021-08-03 09:25:30
  • OfStack

This article describes the example of php magic variable usage, of which __DIR__ is php5.3 new, to share for your reference. The specific usage analysis is as follows:

System constant

__FILE__Current file name
__LINE__Current number of rows
__FUNCTION__Current function name
__CLASS__Current class name
__METHOD__Method name of the current object

Detailed analysis

1. __FILE__

The full path and file name of the file. If used in included files, the included file name is returned. Since PHP 4.0. 2, __FILE__ always contains 1 absolute path (or resolved absolute path if it is a symbolic connection), whereas previous versions sometimes contain 1 relative path.
PHP Constant dirname (__file__)
__FILE__: Known as the PHP magic constant, returns the full path and filename of the currently executing PHP script, including 1 absolute path

1) The dirname (__FILE__) function returns the path where the script is located. Update the network
For example, the file b. php contains the following:

<?php   
$basedir = dirname(__FILE__);  
echo $basedir 
// Will be printed on the page 1 The absolute path of this file ! 
?>


The test I did got the result: E: websiteothertestcms
This is equivalent to the usage of server. mappth in asp
If b. php is referenced by the a. php file require or include in another directory. The contents of the variable $basedir are also the path to the folder where b. php is located. Instead of becoming the directory where the a. php files are located.

2) dirname (__FILE__) 1 returns a directory structure from the current directory of the file to the root of the system.
The current file name is not returned. dirname (__FILE__) may also return 1. (Current directory) [Because the b. php file is in the default WEB directory of the http. conf or PHP configuration development environment

<?php
/**
Set your root directory in your public profile so you don't have to worry about moving frequently.
*/
define('ROOT_PATH', dirname(__FILE__) . DIRECTORY_SEPARATOR);
echo ROOT_PATH;
echo "<br>";
echo __FILE__;
echo "<br>";
echo dirname(__FILE__);
echo "<br>";
echo dirname(dirname(__FILE__));
?>

2. __LINE__

The current line number in the file. This variable is useful in debugging errors, but it is useless in other times, which is purely personal.

<?php
echo __LINE__;  // Show, __LINE__ Line number on which
?>

3. __CLASS__
Class name, PHP5 returns a case-sensitive result
<?php
class base_class
{
function say_a()
{
echo "'a' In fact, in fact, the said the " . __CLASS__ . "<br/>";
}
function say_b()
{
echo "'b' In fact, in fact, the said the " . get_class($this) . "<br/>";
}
}
class derived_class extends base_class
{
function say_a()
{
parent::say_a();
echo "'a' In fact, in fact, the said the " . __CLASS__ . "<br/>";
}
function say_b()
{
parent::say_b();
echo "'b' In fact, in fact, the said the " . get_class($this) . "<br/>";
}
}
$obj_b = new derived_class();
$obj_b->say_a();
echo "<br/>";
$obj_b->say_b();
?>

The results are:
'a'  In fact, in fact, the  said the base_class
'a' In fact, in fact, the said the derived_class
'b' In fact, in fact, the said the  derived_class
'b' In fact, in fact, the said the derived_class

Sometimes we can replace __CLASS__ with get_class

4. __FUNCTION__ and __METHOD__

__FUNCTION__: Function name, the result returned in php5 is case sensitive
__METHOD__: The name of the function in the method, the result returned in php5 is case sensitive
Both of them are the names of the acquisition methods. What's the difference?

<?php
class test
{
function a()
{
echo __FUNCTION__;
echo "<br>";
echo __METHOD__;
}
}
function good (){
echo __FUNCTION__;
echo "<br>";
echo __METHOD__;
}
$test = new test();
$test->a();
echo "<br>";
good();
?>

Return results:
a
test::a
good
good
Compared with isolated functions, both of them can take out the function name, which makes no difference. If it is a method in class, __FUNCTION__ can only take out the method name of class, while __METHOD__ can not only take out the method name, but also take out the class name

5. __DIR__

The directory where the file is located. If used in included files, the directory where the included files are located is returned. It is equivalent to dirname (__FILE__). The directory name does not include the trailing slash unless it is a root directory. (Added in PHP 5.3. 0)
If you want to use __DIR__ in versions before 5.3, you can do this

<?php
if(!defined('__DIR__')) {
$iPos = strrpos(__FILE__, "/");
define("__DIR__", substr(__FILE__, 0, $iPos) . "/");
}
?>

6. __NAMESPACE__

The name of the current namespace (case sensitive). This constant is defined at compile time (ES 142EN 5.3. 0 added)

7. __STATIC__

When you call the static method of class, return the class name, case-sensitive. If called in an inheritance, it returns the inherited class name, whether defined in the inheritance or not.

<?php
//php5.3
class Model
{
public static function find()
{
echo __STATIC__;
}
}
class Product extends Model {}
class User extends Model {}
Product::find(); // "Product"
User::find(); // "User"
?>

Supplement: Magic methods in php

__construct () When an object is instantiated, this method of the object is called first.
__destruct () Calls this method when an object is deleted or when the object operation is terminated.
__get () is called when trying to read a property that does not exist.
__set () is called when an attempt is made to write a value to a property that does not exist.
__call () Calls a method when an attempt is made to call a method whose object does not exist.
__toString () is called when 1 object is printed
__clone () is called when an object is cloned
__isset()
__unset()
__autoload($classname)
__sleep()
__wakeup()

I hope this article is helpful to everyone's php programming.


Related articles: