NotionCommotion Posted June 12, 2018 Share Posted June 12, 2018 (edited) 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; } } Edited June 12, 2018 by NotionCommotion Figured out what I was doing wrong Quote Link to comment https://forums.phpfreaks.com/topic/307370-problems-passing-an-array-of-objects/ Share on other sites More sharing options...
requinix Posted June 13, 2018 Share Posted June 13, 2018 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). Quote Link to comment https://forums.phpfreaks.com/topic/307370-problems-passing-an-array-of-objects/#findComment-1558932 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.