Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在不同控制器中使用静态变量_Php_Codeigniter_Static Variables - Fatal编程技术网

Php 在不同控制器中使用静态变量

Php 在不同控制器中使用静态变量,php,codeigniter,static-variables,Php,Codeigniter,Static Variables,我正在制作一个应用程序,在这个应用程序中,我将用户信息保存在一个静态变量中的CodeIgniter应用程序的用户控制器中,现在我想访问另一个控制器中的静态变量,我如何才能做到这一点?我的代码在这里 <?php class User extends CI_Controller{ public static $user_data = array(); public __construct() {

我正在制作一个应用程序,在这个应用程序中,我将用户信息保存在一个静态变量中的CodeIgniter应用程序的用户控制器中,现在我想访问另一个控制器中的静态变量,我如何才能做到这一点?我的代码在这里

<?php 
     class User extends CI_Controller{
          public static $user_data = array();
          public __construct()
          {
               parent::__construct();
               self::$user_data = array('value'); // values from the model
          }
     }
     // now i can user that static variable from the view in this controller
     class Friends extends CI_Controller{
          public __construct()
          {
               parent::__construct();
               if(User::$user_data->isFriends)
               {
                     redirect('person/'.User::$user_data->id);
               }
          }
     }
     // how can i access this functionality in codeigniter? it gives error undefined class User not found


它不应该是self吗。。。在用户类中?

您的代码存在许多问题

正如您定义的那样,
$user\u data
是一个带有
公共静态$user\u data=array()的数组
User
类的顶部,不需要在第7行重新定义它,即
self::$User\u data=array('value')

第二个问题是
$user\u data
数组将只包含一个元素“value”。我假设根据第15行和第17行(
User:$User\u data->isFriends
User:$User\u data->id
),您正在查找两个元素,“isFriends”和“id”,它们将不存在

这就引出了另一个问题,您用于获取
$user\u data
数组元素的语法将不起作用。后跟属性名称的语法“
->
”用于
对象类型的变量,不能用于关联数组

相反,您应该使用-例如-
$user\u data['isFriends']

我已经在下面更新了你的代码

<?php 
 class User extends CI_Controller{
      public static $user_data = array();
      public __construct()
      {
           parent::__construct();
           // this does the same thing as self::$user_data = array('value');
           // but without unnecessarily declaring the array again.
           self::$user_data[] = 'value';
      }
 }
 // now i can user that static variable from the view in this controller
 class Friends extends CI_Controller{
      public __construct()
      {
           parent::__construct();
           if(User::$user_data['isFriends'])
           {
                 redirect('person/'.User::$user_data['id']);
           }
      }
 }
 // how can i access this functionality in codeigniter? it gives error undefined class User not found

<?php 
 class User extends CI_Controller{
      public static $user_data = array();
      public __construct()
      {
           parent::__construct();
           // this does the same thing as self::$user_data = array('value');
           // but without unnecessarily declaring the array again.
           self::$user_data[] = 'value';
      }
 }
 // now i can user that static variable from the view in this controller
 class Friends extends CI_Controller{
      public __construct()
      {
           parent::__construct();
           if(User::$user_data['isFriends'])
           {
                 redirect('person/'.User::$user_data['id']);
           }
      }
 }
 // how can i access this functionality in codeigniter? it gives error undefined class User not found