0

I have a class called Category which has fields and methods, the array variables are getting mixed up somewhow and I have spent an hour trying to figure this out. Here is the code

        <?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";
    ?>

The parent and desc fields are getting mixed up here and I can't find out why. Please help!

1
  • Why do you have $nc = new Category;?? Instead of removing that line and using $this-> instead of $nc->. And $catObject = new Category(); should be $catObject = new Category; Commented Jun 3, 2013 at 20:45

1 Answer 1

5

Shouldn't this

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

have

$this->desc = $desc;

instead?

Sign up to request clarification or add additional context in comments.

3 Comments

oops! Thank you! sorry for the post then =/
Mark the answer as accepted. (You then see the green checked sign)
As a note, i also realized that desc is a protected word in php

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.