【问题标题】:php Scope Variable from Differents files in class [duplicate]来自类中不同文件的php范围变量[重复]
【发布时间】:2014-04-24 14:55:41
【问题描述】:

我无法访问创建特定文件并由第三个链接使用的变量。
这是一个例子,我有3个文件:
testing.php

<?php 
$special ="foo";
?>

其他.php

<?php
echo $special; // that works

class Testing
{
   public function toto()
   {
     echo $special;//Error considering as undefined
   }
}
?>

main.php

<?php
require 'testing.php';
echo "require work $special";// that works
require 'other.php';

$a = new Testing();
$a->toto();//error
?>

那么我如何才能在我的类Testing 的成员中访问我的变量$special

【问题讨论】:

    标签: php scope


    【解决方案1】:

    $special 变量在函数内不可见。为此,您需要在调用函数时将 $special 变量作为参数传递给函数。

    【讨论】:

      猜你喜欢
      • 2013-09-27
      • 1970-01-01
      • 2015-04-26
      • 2011-05-21
      • 2013-07-28
      • 1970-01-01
      • 1970-01-01
      • 2013-06-11
      • 1970-01-01
      相关资源
      最近更新 更多