php魔术变量用法实例详解_PHP
本文实例讲述了php魔术变量用法,其中__DIR__是php5.3新增的,分享给大家供大家参考。具体用法分析如下:
系统常量
__FILE__ 当前文件名
__LINE__ 当前行数
__FUNCTION__ 当前函数名
__CLASS__ 当前类名
__METHOD__ 当前对象的方法名
详细分析
1. __FILE__
文件的完整路径和文件名。如果用在被包含文件中,则返回被包含的文件名。自 PHP 4.0.2 起,__FILE__ 总是包含一个绝对路径(如果是符号连接,则是解析后的绝对路径),而在此之前的版本有时会包含一个相对路径。
PHP 常量dirname(__file__)
__FILE__ :被称为PHP魔术常量,返回当前执行PHP脚本的完整路径和文件名,包含一个绝对路径
1)dirname(__FILE__) 函数返回的是脚本所在在的路径。 更新网络
比如文件 b.php 包含如下内容:
代码如下:
$basedir = dirname(__FILE__);
echo $basedir
//将在页面打印出一个这个文件所在绝对路径!
?>
我做的测试得到结果: E:websiteothertestcms
这个相当于, asp中的server.mappth的用法
如果b.php被其他目录里的a.php文件require 或者 include 去引用的话。 变量$basedir 的内容还是b.php所在的那个文件夹的路径。 而不是变成a.php文件所在的目录。
2)dirname(__FILE__) 一般会返回文件所的当前目录到系统根目录的一个目录结构。
不会返回当前的文件名称。 dirname(__FILE__) 也可能返回一个 . (当前目录) [原因是 b.php 文件在 http.conf 或者 PHP 配置开发环境的默认WEB目录下
代码如下:
/**
在你的公用的配置文件中,来设置你的根目录,这样就不用担心经常搬家了。
*/
define('ROOT_PATH', dirname(__FILE__) . DIRECTORY_SEPARATOR);
echo ROOT_PATH;
echo "
";
echo __FILE__;
echo "
";
echo dirname(__FILE__);
echo "
";
echo dirname(dirname(__FILE__));
?>
2. __LINE__
文件中的当前行号。这个变量在调试错误的时候,还是比较有作用的,其他的时候,没什么用处,纯属个人观点。
代码如下:
echo __LINE__; //显示,__LINE__所在的行号
?>
3. __CLASS__
类的名称,PHP5返回的结果是区分大小写的
代码如下:
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();
?>
结果为:
代码如下:
'a' – said the base_class
'a' – said the derived_class
'b' – said the derived_class
'b' – said the derived_class
有的时候,我们可以用get_class来代替__CLASS__
4. __FUNCTION__和__METHOD__
__FUNCTION__:函数名称,php5中返回的结果是区分大小写的
__METHOD__:方法中的函数名称,php5中返回的结果是区分大小写的
二个都是取得方法的名称,有什么不同呢?
代码如下:
class test
{
function a()
{
echo __FUNCTION__;
echo "
";
echo __METHOD__;
}
}
function good (){
echo __FUNCTION__;
echo "
";
echo __METHOD__;
}
$test = new test();
$test->a();
echo "
";
good();
?>
返回结果:
a
test::a
good
good
相对于孤立的函数来说,二个都可以取出函数名,没什么区别,如果是class中的方法时,__FUNCTION__只能取出class的方法名,而__METHOD__不光能取出方法名,还能取出class名
5. __DIR__
文件所在的目录。如果用在被包括文件中,则返回被包括的文件所在的目录。它等价于 dirname(__FILE__)。除非是根目录,否则目录中名不包括末尾的斜杠。(PHP 5.3.0中新增)
如果在5.3以前的版本中想用__DIR__的话,可以这样
代码如下:
if(!defined('__DIR__')) {
$iPos = strrpos(__FILE__, "/");
define("__DIR__", substr(__FILE__, 0, $iPos) . "/");
}
?>
6. __NAMESPACE__
当前命名空间的名称(大小写敏感)。这个常量是在编译时定义的(PHP 5.3.0 新增)
7. __STATIC__
当你调用class的静态方法时,返回class名称,区分大小写。如果在继承中调用的话,不管在继承中有没有定义,都能返回继承的class名。
代码如下:
//php5.3
class Model
{
public static function find()
{
echo __STATIC__;
}
}
class Product extends Model {}
class User extends Model {}
Product::find(); // "Product"
User::find(); // "User"
?>
补充:php中魔术方法
__construct() 当实例化一个对象的时候,这个对象的这个方法首先被调用。
__destruct() 当删除一个对象或对象操作终止的时候,调用该方法。
__get() 当试图读取一个并不存在的属性的时候被调用。
__set() 当试图向一个并不存在的属性写入值的时候被调用。
__call() 当试图调用一个对象并不存在的方法时,调用该方法。
__toString() 当打印一个对象的时候被调用
__clone() 当对象被克隆时,被调用
__isset()
__unset()
__autoload($classname)
__sleep()
__wakeup()
希望本文所述对大家的php程序设计有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7
