Jump to content

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;
    }
}

 

Edited by NotionCommotion
Figured out what I was doing wrong
Link to comment
https://forums.phpfreaks.com/topic/307370-problems-passing-an-array-of-objects/
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).

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.