PHP Magic Variable Usage example detailed, PHP Magic instance detailed _php tutorial

Source: Internet
Author: User

PHP Magic Variable Usage examples, detailed examples of PHP Magic example


This article describes the PHP magic variable usage, of which __dir__ is php5.3 new, shared for everyone to reference. The specific usage analysis is as follows:

System constants

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

Detailed analysis

1. __file__

The full path and file name of the file. If used in the included file, returns the file name that is included. From PHP 4.0.2, __file__ always contains an absolute path (if it is a symbolic connection, the resolved absolute path), and the previous version sometimes contains a relative path.
PHP constant DirName (__file__)
__FILE__: called the PHP Magic Constant, returns the full path and file name of the currently executing PHP script, containing an absolute path

1) the dirname (__file__) function returns the path where the script is located. Update Network
For example, the file b.php contains the following content:
Copy the Code code as follows: <?php
$basedir = DirName (__file__);
Echo $basedir
An absolute path to the file will be printed on the page!
?>

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

2) dirname (__file__) typically returns a directory structure of the current directory of the file to the system root directory. The
does not return the current file name. DirName (__file__) may also return one. (current directory) [The reason is that the b.php file is in the default Web directory of the http.conf or PHP configuration development environment
Copy Code code is as follows: <?php
/**
in your public profile, To set up your root directory so you don't have to worry about moving around often.
*/
Define (' Root_path ', DirName (__file__). Directory_separator);
Echo Root_path;
echo "
";
Echo __file__;
echo "
";
echo dirname (__file__);
echo "
";
Echo dirname (DirName (__file__));
?
2. __line__

The current line number in the

file. This variable in debugging error, still more useful, other time, useless, purely personal point of view. The
copy Code code is as follows: <?php
Echo __line__;//display, the line number where the __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__. "
";
}
Function Say_b ()
{
echo "' B ' –said the". Get_class ($this). "
";
}
}
Class Derived_class extends Base_class
{
function say_a ()
{
parent::say_a ();
echo "' A ' –said the". __class__. "
";
}
Function Say_b ()
{
Parent::say_b ();
echo "' B ' –said the". Get_class ($this). "
";
}
}
$obj _b = new Derived_class ();
$obj _b->say_a ();
echo "
";
$obj _b->say_b ();
?
Result:
Copy code code as follows: ' A ' –said the Base_class
' a ' –said the Derived_class
' B ' –sai d The Derived_class
' B ' –said the Derived_class
Sometimes we can use Get_class instead of __class__

4. __function__ and __method__

__function__: Function name, results returned in PHP5 are case-sensitive
__METHOD__: The function name in the method, the result returned in PHP5 is case-sensitive
Two is the name of the method of acquisition, what is the difference?
Copy the Code code as follows: <?php
Class Test
{
function A ()
{
Echo __function__;
echo "
";
Echo __method__;
}
}
function Good () {
Echo __function__;
echo "
";
Echo __method__;
}
$test = new test ();
$test->a ();
echo "
";
Good ();
?>
return Result:
A
Test::a
Good
Good
Compared to the isolated function, two can take out the function name, no difference, if it is a method in class, __function__ can only take out the class method name, and __method__ not only can take out the method name, but also can remove the class name

5. __dir__

The directory where 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, you can
Copy the 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's static method, the class name is returned, which is case-sensitive. If called in inheritance, the inherited class name can be returned regardless of whether there is a definition in the inheritance.
Copy the 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 an object is instantiated, this method of the object is called first.
__destruct () This method is called when an object or object operation is terminated.
__get () is called when attempting 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 () Call this method when attempting to invoke a method that does not exist for an object.
__tostring () is called when an object is printed
__clone () is called when the object is cloned
__isset ()
__unset ()
__autoload ($classname)
__sleep ()
__wakeup ()

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

http://www.bkjia.com/PHPjc/910596.html www.bkjia.com true http://www.bkjia.com/PHPjc/910596.html techarticle PHP Magic Variable usage examples in detail, PHP Magic Example of a detailed example of this article describes the PHP magic variable usage, which __dir__ is php5.3 new, shared for everyone to reference. Specific usage ...

  • 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.