PHP Magic Variable Usage example detailed _php skill

Source: Internet
Author: User
Tags php script

The example of this article tells the PHP magic variable usage, which __dir__ is php5.3 new, share for everyone reference. The specific usage analysis is as follows:

System constants

__file__ Current file name
__LINE__ the current number of rows
__FUNCTION__ the name of the current function
__class__ Current class name
__method__ the current object's method name

Detailed analysis

1. __file__

The full path and file name of the file. If used in the included file, the included file name is returned. Since PHP 4.0.2, __file__ always contains an absolute path (if the symbolic connection is a parsed absolute path), and the previous version sometimes contains a relative path.
PHP constant DirName (__file__)
__FILE__: Known as the PHP magic constant, returns the full path and file name of the currently executing PHP script, including an absolute path

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

Copy Code code as follows:
<?php
$basedir = DirName (__file__);
Echo $basedir
Will print out an absolute path to this file on the page!
?>


The test I did got the result: e:websiteothertestcms
This corresponds to the use of server.mappth in ASP
If b.php is referenced by a.php file require or include in other directories. The contents of the variable $basedir or the path of the folder where b.php resides. Instead of becoming the directory where the a.php file resides.

2) dirname (__file__) typically returns the directory structure of the current directory of the file to the system root directory.
The current file name is not returned. DirName (__file__) may also return one. (current directory) [Cause the b.php file is under the default Web directory of the http.conf or PHP configuration development environment

Copy Code code as follows:
<?php
/**
In your common configuration file, set your root directory so that you don't have to worry about moving around a lot.
*/
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 in debugging error, or more useful, the other time, no use, purely personal point of view.

Copy Code code as follows:
<?php
Echo __line__; Shows the line number where __line__ is located
?>

3. __class__
The name of the class, and the result returned by PHP5 is case-sensitive
Copy Code code as follows:
<?php
Class Base_class
{
function Say_a ()
{
echo "' A ' –said the". __class__. "<br/>";
}
function Say_b ()
{
echo "' B ' –said the". Get_class ($this). "<br/>";
}
}
Class Derived_class extends Base_class
{
function Say_a ()
{
Parent::say_a ();
echo "' A ' –said the". __class__. "<br/>";
}
function Say_b ()
{
Parent::say_b ();
echo "' B ' –said the". Get_class ($this). "<br/>";
}
}
$obj _b = new Derived_class ();
$obj _b->say_a ();
echo "<br/>";
$obj _b->say_b ();
?>

The results are:
Copy Code code as follows:
' A ' –said the Base_class
' A ' –said the Derived_class
' B ' –said the Derived_class
' B ' –said the Derived_class

Sometimes we can use Get_class instead of __class__.

4. __function__ and __method__

__function__: function name, the result returned in PHP5 is case-sensitive
__METHOD__: The function name in the method, and the result returned in PHP5 is case-sensitive
Two is the name of the method of acquisition, what is the difference?

Copy Code code as follows:
<?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 Result:
A
Test::a
Good
Good
Relative to the isolated function, two can take out the function name, no difference, if the method in class, __function__ can only remove the class method name, and __method__ not only can take out the method name, but also can take out the class name

5. __dir__

The directory in which the file resides. If used in the included file, returns the directory where the included files are located. It is equivalent to DirName (__file__). Unless it is a root directory, the name in the directory does not include the trailing slash. (New in PHP 5.3.0)
If you want to use __dir__ in a previous version of 5.3, so be it.

Copy Code code as follows:
<?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 (PHP 5.3.0 new)

7. __static__

When you call the class static method, the class name is returned, case-sensitive. If invoked in inheritance, the inherited class name can be returned, regardless of whether there is a definition in the inheritance.

Copy Code code as follows:
<?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"
?>

Add: Magic method in PHP

__construct () When instantiating an object, this method of this object is invoked first.
__destruct () This method is invoked when an object or object operation is terminated.
__get () is invoked when an attempt is made to read an attribute that does not exist.
__set () is invoked when an attempt is made to write a value to a property that does not exist.
__call () This method is invoked when an attempt is made to invoke a method that does not exist for an object.
__tostring () is called when an object is printed
__clone () Called when the object is cloned
__isset ()
__unset ()
__autoload ($classname)
__sleep ()
__wakeup ()

I hope this article will help you with your PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.