php魔术变量用法实例详解,php魔术实例详解
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











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

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.
