Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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_Arrays - Fatal编程技术网

PHP数组变量混淆

PHP数组变量混淆,php,arrays,Php,Arrays,我有一个名为Category的类,它有字段和方法,数组变量有些混淆,我花了一个小时试图弄清楚这一点。这是密码 <?php /** * This is a Category class * * @author Seraph */ class Category { private $id; private $parent; private $title; p

我有一个名为Category的类,它有字段和方法,数组变量有些混淆,我花了一个小时试图弄清楚这一点。这是密码

        <?php

    /**
     * This is a Category class
     *
     * @author Seraph
     */
    class Category {
        private $id;
        private $parent;
        private $title;
        private $desc;

        protected $newCategories = array();


        public function _construct() {
            $this -> id = NULL;
            $this -> parent = NULL;
            $this -> title = NULL;
            $this -> desc = NULL;
        }

        /*
         * Function to add a new category to the newCategories array
         */
        public function addNewCategory($parent,$title,$desc) {
            $nc = new Category;
            $nc->setId(0);
            $nc->setParent($parent);
            $nc->setTitle($title);
            $nc->setDesc($desc);

            $this->newCategories[$title] = $nc;
        }
        /*
         * Function to remove a category from the newCategories array
         */
        public function removeNewCategory($title) {
            $tempArray = $this->newCategories;
            for($i=0;$i<count($tempArray,1);$i++) {
                $element = $tempArray[$title];
                if($tempArray[$title] == $title) {
                    $tempArray = array_diff($tempArray, array($title));
                }
            }
            $this->newCategories = $tempArray;
        }




        /*
         * Getters and Setters
         */
        public function getId() {
            return $this->id;
        }
        public function setId($id) {
            $this->id = $id;
        }

        public function getParent() {
            return $this->parent;
        }
        public function setParent($parent){
            $this->parent = $parent;
        }

        public function getTitle() {
            return $this->title;
        }
        public function setTitle($title){
            $this->title = $title;
        }

        public function getDesc() {
            return $this->desc;
        }
        public function setDesc($desc){
            $this->parent = $desc;
        }

        public function getNewCategories() {
            return $this->newCategories;
        }


    }

    //TESTER
    $catObject = new Category();
    $catObject->addNewCategory("0", "City Scape", "This is a description of the category.");
    $nc = $catObject->getNewCategories();


    var_dump($nc["City Scape"]);

    echo $nc["City Scape"]->getParent() . "\n";
    echo $nc["City Scape"]->getTitle() . "\n";
    echo $nc["City Scape"]->getDesc() . "\n";
    ?>

父字段和描述字段在这里混淆了,我不知道为什么。请帮忙

这不应该吗

    public function setDesc($desc){
        $this->parent = $desc;
    }

相反?

这不应该吗

    public function setDesc($desc){
        $this->parent = $desc;
    }


相反?

为什么有
$nc=新类别??而不是删除该行并使用
$this->
而不是
$nc->
。和
$catObject=new Category()应该是
$catObject=新类别为什么有
$nc=新类别??而不是删除该行并使用
$this->
而不是
$nc->
。和
$catObject=new Category()应该是
$catObject=新类别哎呀!非常感谢。很抱歉发了这篇帖子,那么就把答案标记为已接受。(然后你会看到绿色的勾选符号)作为说明,我还意识到desc在phpoops中是一个受保护的字!非常感谢。很抱歉发了这篇帖子,那么就把答案标记为已接受。(然后您会看到绿色的勾选符号)作为说明,我还意识到desc在php中是一个受保护的字