search
Homephp教程php手册php中include文件变量作用域的研究,

php中include文件变量作用域的研究,

Jun 13, 2016 am 09:01 AM
includephpScopevariableexistdocumentofResearch

php中include文件变量作用域的研究,

 

在php中我们有时候需要include一个文件。比如我前段时间在写一个框架的时候,打算用原生的php作为模板,然后写一个display方法引入模板文件就可以,但是这只是我的意淫而已。

写完后发现在模板中所有的变量都提示未定义。通过各种研究和查找资料,总结了include文件时的几种情况下的作用域。

 

第一种情况:A文件include B文件,在B文件中可以调用A中的变量。

A文件代码

<?<span>php
 </span><span>$aaa</span> = '123'<span>;
 
 </span><span>include</span> "B.php";

B文件代码:

<?<span>php

</span><span>echo</span> <span>$aaa</span>;

可以正常输出内容。

 

第二种情况:A文件include B文件,然后在A文件中可以调用B文件的变量。

A文件代码

<?<span>php

</span><span>include</span> "B.php"<span>;

</span><span>echo</span> <span>$fff</span>;

B文件代码

<?<span>php

</span><span>$fff</span> = 'i am f';

这个时候是可以正常输出内容的。

 

第三种情况:A文件的某个类的某个方法中调用B文件,然后在B文件中可以调用该方法中的变量。

A文件代码

<?<span>php

</span><span>class</span><span> test{
    </span><span>public</span> <span>function</span><span> show(){
        </span><span>$bbb</span> = 'abc'<span>;
        </span><span>include</span> "B.php"<span>;
    }
}

</span><span>$t</span> = <span>new</span><span> test;
</span><span>$t</span>->show();

B文件的代码

<?<span>php

</span><span>echo</span> <span>$bbb</span>;

这个时候是可以正常输出内容的。

第四种情况:A文件通过定义的一个函数引入B文件,在B文件中无法使用A中的变量,但是可以使用A文件中调用函数(display)中的变量。

A文件代码:

<?<span>php
</span><span>$aaa</span> = '123'<span>;

</span><span>function</span> display(<span>$file</span><span>){
    </span><span>$bbb</span>= 'asdasdas'<span>;
    </span><span>include</span> <span>$file</span><span>;
}

display(</span>"B.php");

B文件代码:

<?<span>php
</span><span>echo</span> <span>$aaa</span><span>;
</span><span>echo</span> <span>$bbb</span>;

运行后$aaa提示未定义,$bbb可以正常输出。

 原文链接:http://www.cnblogs.com/dragondean/

所以我开始准本用一个display方法来引入模板是不可行的。根据方面的三种情况,最后我选择了写一个类来导入模板文件。目前ThinkPHP和Smarty也都是用类来引入模板文件的。文中不足之处欢迎指正。

 

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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

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.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment