Answer the question
In order to leave comments, you need to log in
Code in OOP PHP paradigm?
Write code in the OOP paradigm that conforms to the following structure. Entities: User, Article. Links: One user can write multiple articles. Each article can have only one author user. Functionality: the ability for the user to create a new article; the opportunity to get the author of the article; the ability to get all the articles of a specific user; the ability to change the author of the article. If you have applied any patterns when writing, indicate which ones and for what purpose. Code that implements specific functionality is not required, just the general structure of classes and methods. The code should be commented in PHPDoc style.
I got something like this. Just don’t hit hard, I myself understand that this is hell. But at least tell me, is this at least a little like a task or even by?
<?
// Класс пользователя
class user {
// данные (свойства):
var $name; // имя пользователя
var $id; // id пользователя
// Конструктор, в котором по $id загружаются данные пользователя
// например $this->name
function user($id) {
// т.к. было сказано код функционала не писать, сделаю так...
$this->name = 'test'; // Тут вынимаем из базы имя
$this->id = $id;
}
// создаю новую статью с user_id = $this->id
function creat_article() {
$article = new Article($this->id);
}
// получить все статьи конкретного пользователя
function creat_article() {
$article = new Article; // Создаю без id, чтоб получился "пустой" объект
$article->find_by_user($this->id); // а тут выбираю все статью данного user-а
}
}
// А вот и класс самой статьи
class Article() {
// данные (свойства):
var $user_id; // имя пользователя
var $array_of_article = array(); // Тут буду сидеть все статьи...
// Конструктор
function Article($user_id == nuul) {
// Если в конструктор отправлен id то сразу нахожу его...
if ($user_id) {
$this->user_id = $user_id;
}
}
function find_by_user($user_id == nuul) {
// ищу все статьи данного пользователя...
if ($user_id) {
$this->user_id = $user_id;
}
// Делаю проверку т.к. объект может быть создан без id и в данный метод он может быть не отправлен...
if ($this->user_id) {
// В какой-нибудь массив складываю статьи....
$this->array_of_article[] = ''; // Вынимаю из базы статью и кладу в массив....
}
}
function change_user($new_user_id == nuul) {
if ($new_user_id) {
$this->user_id = $new_user_id;
// И сразу исскуственно вызываю конструктор
Article($new_user_id)
}
}
}
Answer the question
In order to leave comments, you need to log in
You've focused on trying to make your solution database-aware when you weren't asked to. Because of the database-oriented nature, strange things happen in your code, you can say "magic". Some things are generally strange, like a list of articles inside an article.
You were asked to make descriptions of classes, methods and class properties in PhpDoc format , but you didn't.
Try again, but with the following restrictions:
If you are still in doubt, then I advise you to read the Doctrine tutorial . There they gradually create the User and Bug classes, which are completely correct from the OOP point of view. (they do it sequentially, so don't be too lazy to scroll to the very end of the tutorial)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question