dongqing4774 2013-04-16 13:48
浏览 68
已采纳

在同一个类中存储和使用全局变量,但使用php的不同公共函数

I have a project made in netbeans with php and I am using Zend framework, so it's model-view-controller.

Into a class (newclassController), I have 4 public functions. I want to store a global variable from the second function and use it into the 4-th function.

I have tried to send the variable in the URL as a parameter and SESSION variables but I get some errors so if you have some other ideeas please enlight me.

All I tried returns gives this error message:
Notice: Undefined variable

NOTE: It works if I store the variable in the init function, but I want to store it from another function.

  • 写回答

2条回答 默认 最新

  • dongyuqie4322 2013-04-17 09:08
    关注

    unless I miss my guess when you initiate a value in:

    public function init(){
        $this->value = 'value';
    }
    

    the value is available to all of the 'actions' in the controller.

    This is by design.

    The init() method is used in ZF1 to supplement the constructor. It's where you add arguments that might normally be put in the constructor.

    In order to persist a value from one action to another, some form of storage must be used.

    For example:

    //a verbose example
    public function indexAction(){
        //initiate a variable
        $i = 100;
        //start a new session and assign a name to it
        $session = new Zend_Session_Namespace('number');
        //assign the value to the namespace
        $session->value = $i
    }
    

    later in that same controller or even another controller

    public function newAction(){
        //access the session
        $session = new Zend_Session_Namespace('number');
        //assign the value to the view
        $this->view->value = $session->value;
    }
    

    now in the new.phtml view

    <?php echo $this-value ?>
    

    An important thing to remember when using PHP and specifically Zend Framework 1, every request runs the entire application.

    This was a concept that tripped me up in the beginning. Every time you request a new page your Zend Framework application runs from scratch, so any data that needs to survive from one request to the next must be saved (persisted).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度