Jump to content

Can't instantiate PageArray


FrancisChung
 Share

Recommended Posts

Hi there,

I'm having trouble instantiating a PageArray and was wondering if i've overlooked something very obvious (Won't be the first time!)

I've included a snippet of the code 

The Error I'm getting is "Class 'Site\PageArray' not found".

I'm using namespaces as I've had name collisions before.

Still stumbling my way through PHP and any guidance would be much appreciate.

----------------------------

G

namespace Site;
 
 
class Data {
 
    /**
     * Wrapper function to return items inside a PageArray.
     * @param $items
     * @return PageArray - A PageArray containing all the items passed in
     */
    static function PageArrayWithChildren($items)
    {
 
        //$array = new wire(PageArray);
        $array = new PageArray();
        foreach($items as $item)
        {
            $array->import($item->children());
        }
        return $array;
    }

}

Link to comment
Share on other sites

Which version of ProcessWire are you using? I'm guessing 2.x.

Version 3.x has namespaces baked in so might be a better way to go to test things - everything starts off in the ProcessWire namespace I believe: https://processwire.com/blog/posts/processwire-2.6.20-and-surprise-processwire-3.0-alpha-1/#processwire-3.0-alpha-1

That topic also explains that 2.x runs in the root PHP namespace, so what you've done above is set your own namespace and then everything that follows is looking for that namespace, but ProcessWire is in the root PHP namespace (2.x) or in the ProcessWire\ namespace (3.x).

So any advice will depend on the version you're using really.

  • Like 1
Link to comment
Share on other sites

Hi Pete,

Thanks for the prompt reply (on a Sunday!)
I'm using Processwire 2.6.x. I would be hesitant to move to v3.x mainly because it's Alpha software and it's for a site that's already Live at the moment.

I also haven't built enough Unit Tests to be confident to doing such a migration.

You mentioned Root Namespace in your reply, so I tried to instantiate using \PageArray and it seems to have worked.

Thanks again!

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...