Jump to content

Namespaces different behaviour per server environment


gebeer
 Share

Recommended Posts

Hi all,

I read through https://www.php.net/manual/en/language.namespaces.basics.phphttps://www.php.net/manual/en/language.namespaces.rules.php and @MoritzLostvery nice writup here: 

 

Been using PW classloader and namespacing for quite a while. Now for the first time I run into problems. On the local and remote dev environments everything is working as expected. Now moving the site to staging for the first time, I get an E\error Trait not found (see below for exact error in context).

 

Here's my setup all environments on PHP 8.1 with PW 3.0.208:

site/modules/Site/Site.module.php

	public function __construct()
	{
		parent::__construct();
		$this->wire->classLoader->addNamespace("Diff", __DIR__ . "/classes");
	}

site/modules/Site/classes/ResponsiveImages.php

<?php

namespace Diff;

use ProcessWire\Pageimage;
use ProcessWire\WireException;

use function ProcessWire\wireInstanceOf;

trait ResponsiveImages
{

	/**
	 * renders responsive picture tag for specific breakpoints/sizes
	 * used for product description image, ...
	 * @param PageImage $pageImage
	 * @param bool $full optional, default false, if true, sizes attribute will be '100vw'
	 * @return string picture markup
	 */
	public function renderImageFormat1($pageImage, $full = false)
	{
...
     

site/classes/DefaultPage.php

<?php

namespace ProcessWire;

use Diff\ResponsiveImages;
#use RockMigrations\MagicPage;

/**
 * Custom page class that provides properties/methods for all pages
 *
 *
 */

class DefaultPage extends Page
{
	use ResponsiveImages;
	#use MagicPage;

On the staging server this throws the error: Fatal Error: Trait "Diff\ResponsiveImages" not found

if I change the first use statement to `use \Diff\ResponsiveImages;` the error is gone.

In my understanding this doesn't make sense, because with the $wire->classloader the namespace 'Diff' is loaded inside the 'ProcessWire' namespace and not as a direct descendant of the root namespace '\'. Or am I wrong in my assumption? And if it is added to the root namespace as \Diff then it should throw errors in all environments?

EDIT: I see from the ResponsiveImages.php that indeed my namspace Diff seems to live off root namespace. Otherwise my IDE wouldn't have added that namespace import statements for \ProcessWire\Pageimage etc 

I see in Moritz's example that he is defining his namespace explicitely under ProcessWire like 'ProcessWire\\TrelloWire' inside the classloader function. (I think the double backslash is not needed anymore, looking at https://github.com/processwire/processwire/blob/6ff498f503db118d5b6c190b35bd937b38b80a77/wire/core/WireClassLoader.php#L239 ff)

What could be the reason for that server behaving in that way? Like I said, in other environments (and on other projects) that has never been an issue. And I see other modules setup in a similar fashion (like @bernhard's for example). That makes me think that there might be some server setting for that or some PHP module missing on that specific server?

 

Link to comment
Share on other sites

7 hours ago, gebeer said:

That makes me think that there might be some server setting for that or some PHP module missing on that specific server?

I'd be very surprised, but I don't know. I've never had such an issue. Did you check if the classloader is loaded before your use statement fires? Sometimes when working with migrations it might happen that some modules are loaded in a different order (because you installed them in a different point in time) and so you can end up with those kind of "... not found" issues.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...