5

グローバル変数の使用でいくつかのエラーに直面しています。グローバル スコープで $var を定義し、それを関数で使用しようとしましたが、そこではアクセスできません。より良い説明については、以下のコードを参照してください。

ファイル a.php:

<?php

  $tmp = "testing";

  function testFunction(){
     global $tmp;
     echo($tmp);
  }

この関数がどのように呼び出されるかについて少し説明します。

ファイル b.php:

<?php
  include 'a.php'
  class testClass{
    public function testFunctionCall(){
        testFunction();
    }
  }

上記の「b.php」は、次を使用して呼び出されます。

$method = new ReflectionMethod($this, $method);
$method->invoke();

現在、目的の出力は「テスト中」ですが、受信した出力は NULL です。

助けてくれてありがとう。

4

4 に答える 4

4

protected関数の呼び出しに失敗し、キーワードも削除しました。

この方法を試してください

<?php

  $tmp = "testing";

  testFunction(); // you missed this

  function testFunction(){  //removed protected
     global $tmp;
     echo($tmp);
  }

同じコードですが、を使用$GLOBALSすると、同じ出力が得られます。

<?php

$tmp = "testing";

testFunction(); // you missed this

function testFunction(){  //removed protected
    echo $GLOBALS['tmp'];
}
于 2013-09-17T11:19:38.727 に答える
1

この保護された関数は変数にアクセスできません。したがって、protectedを削除して使用します。

<?php

  $tmp = "testing";

   function testFunction(){
     global $tmp;
     echo ($tmp);
  }
于 2013-09-17T11:20:03.613 に答える
0

保護された関数は、次のようなクラスにある必要があります。

 Class SomeClass extends SomeotherClass {

   public static $tmp = "testing";

   protected function testFunction() {

      echo self::$tmp;

   }
}
于 2013-09-17T11:24:04.083 に答える