Answer the question
In order to leave comments, you need to log in
Have I created the class correctly?
Guys, I created the first class in my life. More precisely, two (page and product).
I'm still learning. Please see if there are BAD errors in the grammar of the code. And in general is it OOP style or still procedural?
<?php
class myPage
{
private $h1;
private $content;
private $css = array();
private $js = array();
public function set_h1($text)
{
$this->h1 = '<h1>'.$text.'</h1>';
}
public function append_Content($content)
{
$this->content .= $content;
}
public function insert_Css($res)
{
$this->css[] = $res;
}
private function css_files2html($arr)
{
if(!is_array($arr) || count($arr) == 0)
{
return;
}
static $code;
foreach($arr as $v)
{
$code .= '<link href="'.$v.'" type="text/css" rel="stylesheet" />';
}
return $code;
}
public function createPage()
{
$repl = array(
'{content}' => $this->h1.'<div class="content">'.$this->content.'</div>',
'{links}' => $this->css_files2html($this->css)
);
return strtr(file_get_contents('theme/template.html'), $repl);
}
}
class myProduct
{
private $id;
private $name;
private $price;
public function __construct($id, $name, $price)
{
$this->id = $id;
$this->name = $name;
$this->price = $price;
}
public function __destruct()
{
}
public function product_Create()
{
static $code;
$code = '<div class="product-item-'.$this->id.'">';
$code .= '<h3>'.$this->name.'</h3>';
$code .= '<p>Цена: '.$this->price.' руб.</p>';
$code .= '</div>';
return $code;
}
}
/************* CODE *************/
$page = new myPage();
echo $page->insert_Css('/theme/main.css');
echo $page->insert_Css('/theme/print.css');
$url = 'about/'; //temp
$result = $mysqli->query("SELECT * FROM `pages` WHERE `url` = '".$url."'");
if($result->num_rows == 1)
{
$row = $result->fetch_assoc();
$page->set_h1($row['h1']); //set h1
$page->append_Content($row['text']); //append text of page
}
else
{
$page->set_h1('404 Page not found');
}
$result->close();
//random append content for page
$page->append_Content(file_get_contents('theme/promo_block.html'));
//Вывод будет потом из БД.
$temp_pro = array(
1 => array('name' => 'Товар1', 'price' => '1000'),
2 => array('name' => 'Товар2', 'price' => '4000'),
3 => array('name' => 'Товар3', 'price' => '2000')
);
$product_obj = '';
foreach($temp_pro as $key => $val)
{
$product_obj = new myProduct($key, $val['name'], $val['price']); //create product
$page->append_Content( $product_obj->product_Create() ); // append product
}
echo $page->createPage(); //output page
Answer the question
In order to leave comments, you need to log in
I took a quick look at the code and what caught my eye was the names of the methods, write in the camelCase style, not almost_Camel_Case
insertCSS (you can use Css as you prefer)
appendContent
etc.
I'm especially concerned about whether I fill the variables correctly for return - it's better to minimize return so that it returns some primitive type (boolean, array, int etc.), but not HTML text (read about MVC).
And why prefixes my *? is it better without?
set_h1 is not ice, but what if you later want to change h1 to div#title? abstract, write setTitle or something else.
For the rest - learn and practice, look at the code on github and compare, otherwise your question is somehow incorrect or something
Code style jo..bad short. In principle, we have already written above, I will repeat the points a little.
- Write camelCase style - it's fashionable and beautiful :) (and also recommended by the psr-1 standards, which is much more important).
- Put curly brackets on psr-1 - the code becomes neater.
- Do not spare letters - write long clear names of methods and properties.
- In model objects it is not necessary (read it is impossible) to use pieces of representation (read html and others like it). The model (in this case, the product - the model) stores and processes pure data, display is not its task.
- Reception of the data by the code and transmission in object - yours a nightmare. The object itself must know how to initialize itself, it is enough for it to pass data for initialization (for example, a unique id), the request to the database should ideally occur in a separate object (for example, bd), which is called from the model, the result is returned to the model object. The model is initialized with this data.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question