Hello, I am new to PHP, learning classes and objects. It is necessary to create a class of goods and product categories. For categories, specify the methods for adding a product to a category, the method for displaying a category with all products on the screen, and the method for outputting all categories with all products.

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 () Tell me, please, where are the errors in it?

    1 answer 1

    The extra bracket after the setCategoryGood method, without the closing bracket before displayAllCategory, like this:

     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 errors. Where are you from in the setCategoryGood method in the line

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

    take the price? Maybe it needs to be passed there through a parameter?

    You had problems in that you enclose variables in double quotes, so it is not necessary to do so. And also in if instead of two equal, which are used for comparison, they used one =, which serves for assignment and, therefore, your condition will always be true.

    Also string

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

    You had single quotes, which means that the variables inside will not be processed by the interpreter.

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

    The right decision for your problem

     <?php class Good { protected $name; protected $price; public function __construct($name, $price) { $this->name = $name; $this->price = $price; } public function getName() { return $this->name; } public function getPrice() { return $this->price; } } class Category { protected $id; protected $name; protected $goods; public function __construct($id, $name) { $this->name = $name; $this->id = $name; $this->goods = []; } public function add(Good $good) { $this->goods[] = $good; } public function getName() { return $this->name; } public function getId() { return $this->id; } public function show() { foreach ($this->goods as $good) { echo $good->getName()."-".$good->getPrice()."<br>"; } } } Class Categories { protected $categories; public function __construct() { $this->categories = []; } public function add(Category $category) { $this->categories[] = $category; } public function show() { foreach ($this->categories as $category) { echo "Название категории:".$category->getName()."<br>"; echo "Список товаров: <br>"; $category->show(); echo "<br><br>"; } } } $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 output will be:

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

    I wrote an example based on your task so that you can easily understand the OOP.

    We have 3 classes:

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

    We have a class Good, which in itself has price and name properties, it’s just like in real life: a product has properties. Next, we have a category class that has the properties id and name. These properties are hidden to change from the outside, and to get them, you must use the functions getName, getId, etc., which are defined in the class.

    That is, you can create an instance of the class and display its name:

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

    We also have an array inside Category that will store all the products in the category, and we will add products to it using the add method, and we will only add instances of the Good class. Naturally, to remove all the products from the category, you just need to iterate through the array and refer to the Good class getName and getPrice methods that we have added; the show () method deals with this. Ie, to display all products of a category, you need to add them there and call the show () method.

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

    Next, we have the Categories class, which contains instances of the Category class, about a simple array of categories and a method for displaying show (), the method essentially appeals simply to the methods of the category getName and show () to display information on the 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 now have a little understanding of the PLO.

    • @ Alexey Shimansky there also if ($ good_value = $ cat_name) was in the function setCategoryGood - Firepro
    • one
      Откуда вы в методе setCategoryGood в строчке берете price? - because she does not know that include_once 'good.php' does not add fields of one class to another) - Alexey Shimansky
    • @Firepro thanks! Yes, the $ price and $ good_name variables from the good.php file :) Can you tell me how to add them to catalog.php correctly? - Elena
    • @Elena solve the problem with the price variable and use the good class to the full extent. Pass it in a function and use it, otherwise why do you need it? :) - Firepro
    • @Firepro so the problem is that I cannot figure out how to properly transfer variables from the class good to the category category :) - Elena