search
HomeBackend DevelopmentPHP Tutorial辛星解析动态查看php中的变量、常量、函数、类、文件等信息

     当我们php工作量变大之后,经常需要判断我们是否定义过某个变量,我们是否定义过某个常量,或者说我们是否定义过某个函数等等,我们重复定义常量会导致错误,我们重复定义变量会导致前面的值被覆盖,因此,还是很有必要去了解这些函数的。

    比如说我在我的starphp框架里写一个文件,代码如下:

<?phpecho "当前用户定义的常量";$user_constants = get_defined_constants(TRUE);print_r($user_constants['user']);echo "<br />";echo "当前引入的文件";$files = get_included_files();print_r($files);
然后去浏览器中执行url:http://localhost/starshop/index.php

那么输出的结果为:

当前用户定义的常量Array ( [ROOT] => D:\MyApp\wamp\www\starshop [D] => \ [STAR] => D:\MyApp\wamp\www\starshop\star [CORE] => D:\MyApp\wamp\www\starshop\star\core [HOST] => localhost [APP] => D:\MyApp\wamp\www\starshop\app [LOG] => D:\MyApp\wamp\www\starshop\app\data\log [MODULE] => D:\MyApp\wamp\www\starshop\app\index [VIEW] => D:\MyApp\wamp\www\starshop\app\index\view ) 当前引入的文件Array ( [0] => D:\MyApp\wamp\www\starshop\index.php [1] => D:\MyApp\wamp\www\starshop\star\star.php [2] => D:\MyApp\wamp\www\starshop\star\core\config.php [3] => D:\MyApp\wamp\www\starshop\star\core\fun.php [4] => D:\MyApp\wamp\www\starshop\star\core\core.php [5] => D:\MyApp\wamp\www\starshop\star\core\control.php [6] => D:\MyApp\wamp\www\starshop\app\index\control\index.c.php [7] => D:\MyApp\wamp\www\starshop\app\index\view\index.php )
当然读者并没有我的starphp的源代码,不过不用着急,我只是给大家演示这两个函数的作用,大家可以用手头的框架去演示,看看到底引入了哪些文件和定义了哪些常量,还是蛮不错的。


下面是这些函数的说明:

get_defined_vars:获取用户定义的变量get_defined_functions:获取所有已定义的函数get_loaded_extensions:获取所有可用的模块get_extension_funcs($module_name):获取指定模块的可用函数get_defined_constants():获取所有常量get_declared_classes():获取已定义的类get_included_files():获取导入的文件

感觉还是蛮有用的,因为今天我需要测试我是否定义了一个常量,也记载一下。。。。嘎嘎



Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

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.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools