简体   繁体   English

当没有类范围处于活动状态时,无法访问self ::

[英]Cannot access self:: when no class scope is active

I am trying to use a PHP function from within a public static function like so (I've shortened things a bit): 我试图在公共静态函数中使用PHP函数(我已经缩短了一些东西):

class MyClass {

public static function first_function() {

    function inside_this() {    
            $some_var = self::second_function(); // doesnt work inside this function
    }               

    // other code here...

} // End first_function

protected static function second_function() { 

    // do stuff

} // End second_function

} // End class PayPalDimesale

That's when I get the error "Cannot access self:: when no class scope is active". 那是我收到错误“无法访问自我::当没有类活动范围时”。

If I call second_function outside of the inside_this function, it works fine: 如果我在inside_this函数之外调用second_function ,它可以正常工作:

class MyClass {

public static function first_function() {

    function inside_this() {    
            // some stuff here  
    }               

    $some_var = self::second_function(); // this works

} // End first_function

protected static function second_function() { 

    // do stuff

} // End second_function

} // End class PayPalDimesale

What do I need to do to be able to use second_function from within the inside_this function? 为了能够在inside_this函数中使用second_function ,我需要做什么?

That is because All functions in PHP have the global scope - they can be called outside a function even if they were defined inside and vice versa. 这是因为PHP中的所有函数都具有全局范围 - 即使它们是在内部定义的,它们也可以在函数外部调用,反之亦然。

So you have to do: 所以你必须这样做:

 function inside_this() {    
   $some_var = MyClass::second_function(); 
 }     

Works with PHP 5.4: 适用于PHP 5.4:

<?php
class A
{
  public static function f()
  {
    $inner = function()
    {
      self::g();
    };

    $inner();
  }

  private static function g()
  {
    echo "g\n";
  }
}

A::f();

Output: 输出:

g

Try changing your first function to 尝试将您的第一个功能更改为

public static function first_function() {

    $function = function() {    
            $some_var = self::second_function(); //  now will work
    };               
    ///To call the function do this
    $function();
    // other code here...

} // End first_function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 致命错误:{address}中没有活动的类范围时,无法访问self :: - Fatal error: Cannot access self:: when no class scope is active in {address} php错误,当没有类作用域处于活动状态时,无法访问self :: - Php error, Cannot access self:: when no class scope is active Codeigniter无法访问self ::当帮助器中没有活动的类范围时 - Codeigniter Cannot access self:: when no class scope is active inside a helper 什么是致命错误的解决方案:当avada主题中没有类范围活动时,不能使用“self”? - What is solution to Fatal error: Cannot use “self” when no class scope is active in avada theme? 无法访问父级:: 当前 class scope 没有父级时 - cannot access parent:: when current class scope has no parent PHP致命错误:无法访问父类::当当前类范围在cakephp 3.x中没有父类时 - PHP Fatal error: Cannot access parent:: when current class scope has no parent in cakephp 3.x 致命错误:无法访问父级 :: 当当前类作用域中没有父级时 - Fatal error: Cannot access parent:: when current class scope has no parent in 使用::范围运算符无法从实例访问类常量 - Cannot access class constant from an instance using the :: scope operator 访问自类常量 - Access Self Class Constants 范围问题:在类中调用时,必需的文件无法访问变量 - Scope issue: required file cant access variables when called in class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM