0

I saw that the conversion from a class instance (object) to associative array is a simple cast :

$array =  (array) $yourObject;

So if i have class A and an associative array contaning the neccesary fields to fill a class A object would it be enough to do the opposite? e.g

$aInstance=(A)$assocArray;

If not whats the easiest way to do so?

Ofek Ron
  • 8,354
  • 13
  • 55
  • 103

1 Answers1

0

Things are not that easy, since there is no way for php to somehow magically guess what to do with the values of that array. Objects are a structured set of data unlike an array. So you need a description of the structure and rules how to use it.

You need to implement a constructor in the class definition, then you can construct a new object based on some given array. Take a look at this arbitrary example:

<?php
class A {
    protected $title;
    protected $data;
    public function __construct ($data) {
        $this->title = $data['title'];
        $this->data = sprintf('this is foo: %s', $data['foo']);
    }
    public function __toString() {
        return sprintf(
            "I am an object titled '%s'\nI hold that foo value: %s\n",
            $this->title,
            $this->data
        );
    }
}

$data = [
    'title' => 'hello world',
    'foo' => 'foovalue',
    'bar' => 'barvalue'
];
$object = new A($data);
echo $object;
var_dump($object);

The obvious output is:

I am an object titled 'hello world'
I hold that foo value: this is foo: foovalue
/home/arkascha/test.php:25:
class A#1 (2) {
  protected $title =>
  string(11) "hello world"
  protected $data =>
  string(21) "this is foo: foovalue"
}
arkascha
  • 41,620
  • 7
  • 58
  • 90
  • it would make sense to me that it would not be hard for php's creator to guess that casting an associative array to a class instance would attempt to create a new instance of that class filling all its fields using fields names as keys. this just make sense. now every class i make i have to copy paste code... but thanks! – Ofek Ron Oct 15 '16 at 11:03
  • @OfekRon No, that is not the idea behind object oriented programming. What you describe is exactly what arrays are for. Why double that principle with objects? That does not make sense, in contrary, it only motivates to _not_ take advantage of OOP but to use objects as stupid, passive data containers. – arkascha Oct 15 '16 at 11:06
  • It may make sense for some use cases, most use cases for me. may not make sense if your variables are private and you do some logic on setters. but i think some default operation could be helpfull here. anyway, I used the cosntructor as you suggeted. Thanks – Ofek Ron Oct 15 '16 at 11:19
  • @OfekRon Indeed it appears to be an intuitive approach, but still I think that is a false hope. There is a very good principle in informatics: "prefer explicit over implicit". That is especially true for OOP. Nothing is more annoying than implicit behavior if you have to work with code you did not just code a few minutes ago. And I dare say: those few minutes to implement simple basic methods into a class definition are not really the issue. However maybe you also want to take a look at phps `stdClass`. – arkascha Oct 15 '16 at 11:35
  • problem with such boilerplate code is that minutes add up to hours which adds up to days and so on... In my humble opinion the dont repeat yourself principle is much more productive than "prefer explicit over implicit". btw if you prefer explicit over implicit, php is not the first choice - a strict type safe langauge such as java or c++ seem more following to that direction... – Ofek Ron Oct 15 '16 at 11:41