Class method error - need help.



  • Hello, I'm new to PHP, studying classes and objects. We need to create a class of goods and goods. For categories, apply methods of adding goods to the category, the method of removing all goods on screen and the method of removing all categories of goods.

    There are classes.

    good.class.php

    class good{
     var $good_name;
    

    var $price;

    var $good_value; //для распределения по категориям

    function setGood ($good_name, $price, $value) {

    $this->good_name = $good_name;
    $this->price = $price;
    $this->good_value = $value;
    

    }

    }

    category.class.php

    include_once 'good.php';

    class category {

    var $cat_id;
    var $cat_name;
    

    function setCategoryGood($good_name) {

        if ($good_value=$cat_name) {
            $category = array("$good_name"=>"$price");
        }
    }
    
    }
    
    function displayCategory($cat_name){
        $this->cat_name=$cat_name;
            foreach ($category as $good_name=>$price) {
            echo $good_name." - ".$price;
        }
    
    function displayAllCategory(){
        $all_category = array("$cat_name" => "$category");
        foreach ($all_category as $cat_name => "$category") {
            foreach ($category as $good_name => $price) {
                echo  $cat_name.'</br>
                <li>$good_name - $price</li>';
            }
        }
    }
    

    }

    index.php

    require_once('good.php');

    require_once('category.php');

    $good1 = new good();

    $good1->setGood ('rose', '10$', 'flower');

    $cat1 = new category();

    $cat1 ->cat_name='flower';

    $cat1->setCategoryGood('flower');

    $cat1->displayCategory('flower');

    This code does not work 🙂 Fatal error: Call to undefined method category::displayCategory()
    Can you tell me where his mistakes are?



  • The lower bracket after the setCategoryGood method, the isolation of the closing brackets before displayAllCategory, is correct:

    include_once 'good.php';
    

    class category {
    var $cat_id;
    var $cat_name;

    function setCategoryGood($good_name) {
        if ($good_value==$this->cat_name) {
            $category=array($good_name=>$price);
        }
    }
    
    function displayCategory($cat_name){
        $this->cat_name=$cat_name;
         foreach ($category as $good_name=>$price) {
            echo $good_name." - ".$price;
        }
    }
    
    function displayAllCategory(){
        $all_category = array($cat_name => $category);
        foreach ($all_category as $cat_name => $category) {
            foreach ($category as $good_name => $price) {
                echo  $cat_name."</br><li>$good_name - $price</li>";
            }
        }
    }
    

    }

    Elena, your code is full of mistakes. Where are you in the setCategoryGood method in the line?

    $category=array($good_name=>$price);

    Take the price? Maybe we should get him through the parameter.

    You've had a problem with you two making variables, so you don't have to. Also addition, if the two equals used for comparison were used by one = which serves for imputation, and therefore, you will always have a true condition.

    Also line

    </br><li>$good_name - $price</li>

    You've been in solitary skirts, which means that the peppers inside will not be processed by an interpreter.

    You also created a good class, but you didn't use it anywhere.

    The right thing to do with your task.

    <?php

    class Good {

    protected $name;
    protected $price;
    
    public function __construct($name, $price) {
        $this-&gt;name = $name;
        $this-&gt;price = $price;
    }
    
    public function getName() {
        return $this-&gt;name;
    }
    
    public function getPrice() {
        return $this-&gt;price;
    }
    

    }

    class Category {

    protected $id;
    protected $name;
    protected $goods;
    
    public function __construct($id, $name) {
        $this-&gt;name = $name;
        $this-&gt;id = $name;
        $this-&gt;goods = [];
    }
    
    public function add(Good $good) {
        $this-&gt;goods[] = $good;
    }
    
    public function getName() {
        return $this-&gt;name;
    }
    
    public function getId() {
        return $this-&gt;id;
    }
    
    public function show() {
        foreach ($this-&gt;goods as $good) {
            echo $good-&gt;getName()."-".$good-&gt;getPrice()."&lt;br&gt;";
        }
    }
    

    }

    Class Categories {

    protected $categories;
    
    public function __construct() {
        $this-&gt;categories = [];
    }
    
    public function add(Category $category) {
        $this-&gt;categories[] = $category;
    }
    
    public function show() {
        foreach ($this-&gt;categories as $category) {
            echo "Название категории:".$category-&gt;getName()."&lt;br&gt;";
            echo "Список товаров: &lt;br&gt;";
            $category-&gt;show();
            echo "&lt;br&gt;&lt;br&gt;";
        }
    }
    

    }

    $category = new Category("flowers", 1);
    $category->add(new Good("flower", 15));
    $category->add(new Good("flower", 25));
    $category->add(new Good("flower", 32));

    $categories = new Categories();
    $categories->add($category);

    $categories->show();

    The conclusion will be:

    Название категории:1
    Список товаров:
    flower-15
    flower-25
    flower-32

    I wrote an example on the basis of your task to make it easy for you to understand the PLO.

    We have three classes:

    1. Good - class for goods
    2. Category - Category
    3. Categories - category

    We have a class of good that has a price and name in itself, it's like a real life: the product has its properties. Next, we have a class that has id and name properties. These properties are hidden to change from outside, and they need to use the functions getName, getId, etc. defined in the class.

    I mean, you can create a copy of the class and remove his name:

    $good = new Good("flower", 15)
    echo $good->getName(); //выведет flower

    We also have in Category a mass that will store all goods in the category, and we will add the goods by adding and add only copies of Class Good. Naturally, in order to remove all goods from the category, we just need to move the mass and turn to the methods getName and getPrice class Good that we have added, this is the method show(s). To remove all goods in the category, they need to be added and show(s) triggered.

    $good = new Good("flower", 15); //создаем товар flower с ценой 15
    $category = new Category("flowers", 1); //создаем категорию с названием flowers и id = 1
    $category->add($good); //добавляем товар в категорию
    $category->show(); //выводим все товары

    Furthermore, we have a Categories class that contains copies of Class Category Category Category, a simple set of categories and a method of withdrawal show(s), the method essentially simply refers to class getName methods and show(s) to extract information in category.

     $good = new Good("flower", 15); //создаем товар flower с ценой 15
    $category = new Category("flowers", 1); //создаем категорию с названием flowers и id = 1
    $category->add($good); //добавляем товар в категорию
    $categories = new Categories(); //Создаем обьект категорий
    $categories->add($category); //Добавляем категорию
    $categories->show(); //Делаем вывод всех товаров и категорий

    I hope you have a little understanding of the PLO now.


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2