Answer the question
In order to leave comments, you need to log in
What is the best way to compose a constructor in a class or should it be a new function?
I continue to gradually and smoothly get involved in OOP ... and try to separate php from html - maybe in the end I will come to mvc. It’s crooked, of course, while it’s far from everything, but there is progress. I didn’t really get a single answer to my previous question, I’ll try to ask in a new context.
While there is a handler class (connections and something like CRUD)
Then I make a selection from the database and FETCH_CLASS.
return $stmt->fetchAll(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, $class); //указываю BDAY
class Entity {
function __set($prop, $value) {
}
}
class Bday extends Entity {
public $birthday=null;
public $surname=null;
public $age=null;
public $days=null;
// function __construct() {
// // $bday=new DateTime($this->birthday);
// // $today = new DateTime("today");
// // $age =$bday->diff($today)->y;
// // $bday->setDate(date("Y"), $bday->format("m"), $bday->format("d"));
// // // $diff =$bday->diff($today)->d;
// // $this->age=$age;
// // $this->days=$bday->diff($today)->d;
// $this->days="1";
// }
}
$data=$obj->db_select($fields, $table, $diff_where, $fieldname=null, $id=null, $order, $class);
foreach ($data as $person) {
$bday=new DateTime($person->birthday);
$today = new DateTime("today");
$age =$bday->diff($today)->y;
$bday->setDate(date("Y"), $bday->format("m"), $bday->format("d"));
$diff =$bday->diff($today)->d;
$person->age=$age;
$person->days=$bday->diff($today)->d;
}
class Entity {
function __construct() {
$this->_attributes = func_get_args();
}
function __set($prop, $value) {
if (in_array($prop, $this->_attributes)) {
$this->$prop = $value;
}
}
}
class Person extends Entity {
public $_attributes;
}
Answer the question
In order to leave comments, you need to log in
class Entity {
function __set($prop, $value) {
$this->$prop = $value
}
}
The __set method, if I'm not mistaken, works when there are no such properties, but you have them. It is better to study some kind of ready-made framework =) For Jobs, I will advise either YII or laravel.
Go from the opposite - how do you want to make requests? Let's say I have something like this:
$users = Model_User::getTools()->findAll(['status' => 'public']);
At the output, we get an array of objects with users
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question