Jump to content

Problems passing an array of objects


NotionCommotion

Recommended Posts

After running the following script, I received an error that I cannot instantiate abstract class which resulted on the last line of the Collection class.  Okay, I get that "self" is the current file.  So, then I changed from "self" to "static".  This is what I should be doing if I wanted to create a new SeriesCollection, right?  But PHP is now complaining that I am passing Collection::__construct() an array instead of an instance of Node. But as far as I can tell, I am passing in an array of Node, and that works perfect as performed earlier in my SeriesMapper class.    Any idea where I am going wrong?  Thanks

Edit.  Dah!!!  I forgot those dang three dots!  Please still confirm that keywork "static" should be used to call not self but the actual instantiated class.

 

$collection=new SeriesCollection(...$this->seriesMapper->read());   //returns an array of Node
$collection->doSomething();
$changes=$collection->getChanges(); //Should return a SeriesCollection

 

abstract class Collection
{
    protected $container, $blueprint;

    public function __construct(Node ...$nodes) {
        $this->container=$nodes;
        $this->blueprint=$nodes;
    }

    public function getChanges($getChildChanges=false){
        $added = array_udiff($this->container, $this->blueprint, function ($o1, $o2) {
            return $o1->id - $o2->id;
        });
        $removed = array_udiff($this->blueprint, $this->container, function ($o1, $o2) {
            return $o1->id - $o2->id;
        });
        $intersect=array_uintersect($this->container, $this->blueprint, function ($o1, $o2) {
            return $o1->id - $o2->id;
        });
        $changedArr=[];
        foreach($intersect as $node) {
            if($changed=$node->getChanges($getChildChanges)) {
                $changedArr[]=$changed;
            }
        }
        return (object)['added'=>new self($added), 'removed'=>new self($removed), 'changed'=>new self($changedArr)]; //Each are arrays of Nodes
    }

    //other methods...
}
class SeriesCollection extends Collection{}
class Node
{
    public function getChanges($getChildChanges=false){
        $current=[];
        foreach($this->getClassPropertyNames() as $name) {
            $current[$name]=$this->$name;
        }
        if($changes=array_diff($current, $this->blueprint)){
            $this->changes=$changes;
            return $this;
        }
    }
}
class SeriesMapper {

    public function read() {
        //...
        $collection=[];
        foreach($stmt as $node) {
             $collection[]=new SeriesNode($node);
        }
        return $collection;
    }
}

 

Link to comment
Share on other sites

2 hours ago, NotionCommotion said:

Edit.  Dah!!!  I forgot those dang three dots!  Please still confirm that keywork "static" should be used to call not self but the actual instantiated class.

Correct. "self" is like __CLASS__ while "static" is like get_class($this).

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.