小弟最近自己写了个mvc框架基本代码如下单一入口index.php:
<?php
require_once("init.php");
require_once("controllers/index.php");
$Action = new $index();
$Action->$indexAction();
?>init.php
<?php
session_start();//不都是require_once吗。那是不是说整个程序的session都启动了。
?>test.php
<?php
sesstion_start();
$_session['test'];//模拟给session赋值
?>controllers/index.php
<?php
class index{
 function indexAction(){
   echo $_session['test'];//这里获取不到session值。我在这个页面的任何地方加sesstion_start()都不行
 }
}
?>test2.php
<?php
sesstion_start();
echo $_session['test'];//这里就能显示,证明session已经有值了,只不过我在类中调不出来
?>
以上就是我遇到的问题,万望各位前辈给我指点个明路。要不这个十一都过不踏实谢谢了

解决方案 »

  1.   

    不是 $_session
    而是 $_SESSIONphp 的变量是大小写敏感的
      

  2.   

    你自己的代码问题,和是否MVC没有任何关系
      

  3.   

    $Action = new $index();
    $index()是什么没见过这么写的一个变量+()奇怪啊
      

  4.   


    这东西都我手误了。。本地程序里是没有问题的。
    我是想知道我样在
    index.php里边require_once('init.php'),而init.php里有session_start(),能否影响到全局。现在看来是否定的。我想知道解决方法。
      

  5.   


    controllers/index.php
    <?php
    class index{
     function indexAction(){
       echo $_session['test'];//这里获取不到session值。我在这个页面的任何地方加sesstion_start()都不行
     }
    }
    ?>在controllers/index.php中自定义的一个类
      

  6.   

    最简单的动态调用方法吧。
    但需要传参数时,这个方法就得挂了。
    建议使用 call_user_func_array() 来调用吧。
      

  7.   


    class A {}
    $index = 'A';
    $a = new $index();
      

  8.   


    why?
    class A {
    function __construct($str) {
    echo $str;
    }
    }
    $index = 'A';
    $a = new $index('asdasdasd');
      

  9.   

    各位大虾。。小弟只是想知道按现在这个程序流程,调用不到session,我该怎么解决可能有几个地方有错误。我更正下。
    index.php中
    $Action = new $index();
    应该为
    $Action = new index();test.php和test2.php中
    sesstion_start();
    应该为
    session_start();这几个都是我刚才着急发帖手误打错了。。大家不要揪着这几个问题不放。谢谢了
      

  10.   

    貌似以前见过这样的,可是$index是个变量和()能直接结合么?
      

  11.   

    楼主获取不到session是不是页面一片空白啊
      

  12.   

    $_session['test'];//模拟给session赋值
    这没赋值吧  $_session['test']=‘test;
      

  13.   

    这段程序只是我简化了一个大概的流程。可能有不完全的地方。楼上那个我都注释了//模拟给session赋值了。。说明我程序里肯定赋值了只是让大家直观的知道我的程序倒底是怎么运行的一个流程。
      

  14.   

    单一入口index.php:
    <?php
    require_once("init.php");
    require_once("controllers/index.php");
    $Action = new index();
    $Action->indexAction();
    ?>init.php
    <?php
    session_start();//不都是require_once吗。当然整个程序的session都启动了。
    ?>controllers/index.php
    <?php
    class index{
     function indexAction(){
       echo $_SESSION['test'];
     }
    }
    ?>
    test.php
    <?php
    sesstion_start();
    $_SESSION['test'] = 'test';//模拟给session赋值
    ?>
      

  15.   

    被调用的方法,参数个数是不确定的,你乍样传递参数??
    所以这个方法只能确定方法的参数。
    一般都会使用 call_user_func_array()来实现的。例如 CI 框架就是了。
      

  16.   

    是不是你的MVC是一个函数包着另一个函数?
    function包function的层模式的.这样要传递全局变量的...        //带入全局变量
            foreach ($GLOBALS as $k=>$v) {
                global $$k;
            }