Brief discussion on the scope _php instance of include file variable in PHP

Source: Internet
Author: User

In PHP, we sometimes need to include a file. For example, I spent some time in writing a framework, I intend to use native PHP as a template, and then write a display method to introduce template files can, but this is just my imagination.

When you finish writing, you find that all the variables in the template are not defined. Through a variety of research and search data, summed up the include file in several cases of the scope.

In the first case: A file include B file, in the B file can call the variables in a.

A file code:

<?php
 $aaa = ' 123 ';
 
 Include "b.php";

B File Code:

<?php

Echo $aaa;

Can output the content normally.

In the second case: A file include B file, and then in a file, you can call the variables of the B file.
A file code:

<?php

include "b.php";

Echo $fff;

B File Code:

<?php

$fff = ' I am f ';

This time it is normal to output the content.

In the third case: A file in a method of a class that calls the B file, and then in a B file, you can call the variable in the method.
A file code:

<?php

class test{public
  function Show () {
    $bbb = ' abc ';
    Include "b.php";
  }
}

$t = new test;
$t->show ();

The code for the B file:

<?php

Echo $bbb;

This time it is normal to output the content.

The fourth situation: A file introduces the B file through a defined function, and the variables in a are not available in the B file, but you can use the variables in the a file called functions (display).
A file code:

<?php
$aaa = ' 123 ';

function display ($file) {
  $bbb = ' Asdasdas ';
  Include $file;
}

Display ("b.php");

B File Code:

<?php
echo $aaa;
Echo $bbb;

$AAA prompts are undefined after running, $BBB can output normally.

So I started with a display method to introduce the template is not feasible. According to the three kinds of situations, I finally chose to write a class to import the template file. Currently thinkphp and Smarty also use classes to introduce template files. The deficiencies in the article are welcome to correct me.

The above mentioned is the entire content of this article, I hope you can enjoy.

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.