PHP在foreach中的switch内设置变量

时间:2013-06-18 09:31:24

标签: php switch-statement scope

我需要在函数中声明一组变量,这些变量基于使用foreachswitch语句与另一个foreach循环的数据循环。我想我误解了我正在使用的变量的范围,任何帮助都会很棒。

class Users {

    public function createUserData(){
        $user = $this->getUserData(); //not shown function
        $this->createFullDataSet($user);
    }

    private function createFullDataSet($user){
        foreach( $user['meta'][0] as $key => $value) {
        //variables required later
        $entity_def_id = 0;
        $entity_id = 0;
        $data_def_id = 0;
        $user_id = 0;
        //thats plenty, you get the idea

        switch( $key ){
           case "id":
               //set $user_id to use later
               $user_id = $value; // <<-- DOESN'T WORK, only works within the case
               break;
           case "email":
           case "username":
           case //lots of other cases...
               break;
           case "location":
           case "hometown":
           case "something":
               //for the last three, the data structure is the same, good test case
               //foreach starts when certain conditions met, irrelevant for question
               foreach( $value as $data_key => $data_value ){
                   $data_type = 'string';
                   if( is_numeric( $data_value )
                       $data_type = 'integer';

                   $data_def_id = $this->createDataDef( some $vars ); //returns an ID using $pdo->lastInsertId(); ( works as has echo'd correctly, at least within this case )

                   $this->createSomethingElse //with variables within this foreach, works
               }
               break;
            } //end of switch
            $this->createRelation( $data_def_id ); // <<-- DOESN'T WORK!! Empty variable
        }
    }

    private function createRelation( $data_def_id ){
        // something awesome happens!
    }
}

从上面的代码中可以看出,我想在switch语句之外使用一个变量,虽然它需要在foreach - &gt; switch - &gt; {{1}中声明由于现有的数据结构(这种数据结构是一种痛苦,这就是为什么需要这样做,在任何人要求之前:不能“只是改变以使其更容易”)。

现在我一直在阅读foreach和switch语句的变量范围(herehereherehere,并试图找到更多信息),然而,在设置为foreach的函数开头的$data_def_id为什么不会重置为内部0中出现的任何值时,没有更明智的。我正在尝试避免使用foreach变量,因为某些功能将用于产品中。

我需要能够在整个私有函数(包括globalprivate function等)中使用foreach中的变量。我做错了什么,我该如何解决?

2 个答案:

答案 0 :(得分:1)

好的找到了答案。

class Users {

    public function createUserData(){
        $user = $this->getUserData(); //not shown function
        $this->createFullDataSet($user);
    }

    private function createFullDataSet($user){//variables required later
        static $entity_def_id = 0; //static within function instead of non-static within foreach
        static $entity_id = 0;
        static $data_def_id = 0;
        static $user_id = 0;
        //thats plenty, you get the idea
        foreach( $user['meta'][0] as $key => $value) {
            //remainder of that method with switch( foreach () )
        }
    }

    private function createRelation( $data_def_id ){
        // something awesome happens!
    }
}

通过在函数中将变量声明为static,整个函数(包括其中的方法)都可以使用变量。给我足够长的时间:s。

答案 1 :(得分:0)

如何将它们定义为class properties

class User {

    private $data_def_id;

    private function createFullDataSet($user){
        //....
        $this->data_def_id = $this->createDataDef( some $vars );

    }
}