D
D
dk-web2015-08-17 12:21:46
PHP
dk-web, 2015-08-17 12:21:46

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

I do this through an empty entity class, which filters all incoming fields for me.
class Entity {
  function __set($prop, $value) {
     }
}

Next, I hammer in the Bday class.
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";
  // }
}

and now the commented out code does not work (what's wrong?
you have to do it as follows - in the handler...
$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;
}

Accordingly, the question is how can I push this foreach into a class in order to already receive a ready copy .. The
truth is somewhere nearby ... but ...
Much appreciated
UPD:
In general, everything is clear ... writing to the class when selecting SELECT * and FetchALL it is dead number - it is necessary to transfer accurate fields to SELECT each time, but it is nonsense kmk. If you need to make one! request and only then break it into different objects .. probably I still don’t quite understand it.
What could be easier? there are 20 fields in the table - I take everything ... but I need to put only 4 defined fields in the person object ... 4 and not 20 ..
there is another option ...
in the handler ...
public $params = array('surname' ,'id','country','status' );
return $stmt->fetchAll(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'person',$this->params);
after
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;
 }

But then this property _attributes is added to the object (
okay... since there are no other solutions - for now I'll do how it works - otherwise I'm too stuck

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Ivanov, 2015-08-17
@Writerim

class Entity {
  function __set($prop, $value) {
      $this->$prop = $value
     }
}

read about setters and getters.
You seem to be passing data to the setter to assign properties, but it does nothing for you.
If you need a setter to work only in assigning values ​​to properties without any logic, then it is better to remove it.

A
Arman, 2015-08-17
@Arik

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 question

Ask a Question

731 491 924 answers to any question