timber / starter-theme

The "_s" for Timber: a dead-simple theme that you can build anything from
MIT License
818 stars 278 forks source link

Fatal error when importing namespaced class #122

Closed DanielWahl closed 3 years ago

DanielWahl commented 3 years ago

Hi, I can't use the StarterSite (1.x) with namespaces, to extend or use other classes.

I'm trying to create a new object from my class Backend (to enqueue wp-styles, etc), but after creating a new instance, I'm getting the "There has been a critical error on this website"-error.

use themename\Backend;
class StarterSite extends Timber\Site {
    private $backend;
    public function __construct() {
        ...
        $this->backend = new Backend();
    }
}

causes the error.

Even trying to move the StarterSite class to a new file (f.e. src/StartSite.php) and creating the instance in functions.php breaks everything.

szepeviktor commented 3 years ago

Hello!

Cannot use "use" and namespaces

Please change the title to Fatal error when importing namespaced class

  1. Is this v1 or v2?
  2. Is the above code in /functions.php ?
  3. Does that file have a namespace ...; statement in it?
  4. What is the error message? Try running your theme with WP-CLI: https://github.com/szepeviktor/wordpress-plugin-construction/blob/master/wp-cli-run-frontend.php
DanielWahl commented 3 years ago
  1. I use v1 / master-branch
  2. Yes, the above code is in /functions.php and I cannot get it working in another file
  3. Currently no namespace. Typing namespace themename; already breaks the page.
  4. When I use the StarterSite-class in a new file and call the class in functions.php with new \crla\StarterSite(); I get the following errorPHP Fatal error: Uncaught Error: Class 'crla\StarterSite' not found in ~/wp-content/themes/crla-theme-master/functions.php:65. After require("src/StarterSite.php") and use clra/StarterSite, I don't get any error running wp-cli-run-frontend, but the page is still broken. PhpStorm finds every Class from the right directory (Timber from /vender/timber/timberlib/Timber.php and StarterSite from src/Startersite.php.

When I change the namespace of functions.php, declare Timber as use \Timber\Timber;, I don't get any error running wp-cli-run-frontend, but the page is still broken.

szepeviktor commented 3 years ago

Please try this.

functions.php

// No namespace!

use crla\StarterSite;

require __DIR__ . '/src/StarterSite.php'; // or Composer's autoloader

new StarterSite();

src/StarterSite.php

namespace crla;

use Timber\Site as TimberSite;

class StarterSite extends TimberSite {
DanielWahl commented 3 years ago

No error, only a warning (Warning: Some code is trying to do a URL redirect.) running the wp-cli, but the page remains broken.

I tried cloning a completely new Starter-Theme v1 and only changing the functions.php by copy&pasting the StarterSite-class into a new file and configuring as above. Still the same, only a warning and page broken.

szepeviktor commented 3 years ago

Warning: Some code is trying to do a URL redirect.

You may need to check --url option to make sure it is a canonical URL.

Doesn't new WordPress versions display the actual error when running through a webserver? Try setting WP_DEBUG to true.

DanielWahl commented 3 years ago

When I leave the StartSite-class inside functions.php and the page is working, I get the exact same warning running wp-cli.

szepeviktor commented 3 years ago

The --url option may specify a non-canonical URL and WordPress does a redirect to the canonical one.

DanielWahl commented 3 years ago

wp --url="http://wp.crla.danielwahl.lu/" eval-file wp-cli-run-frontend.php

Warning: Some code is trying to do a URL redirect. Backtrace:
#0  WP_CLI\Utils\wp_redirect_handler(https://wp.crla.danielwahl.lu/) called at [/var/www/vhosts/danielwahl.lu/wp.crla.danielwahl.lu/wp-includes/class-wp-hook.php:294]
#1  WP_Hook->apply_filters(https://wp.crla.danielwahl.lu/, Array ([0] => https://wp.crla.danielwahl.lu/,[1] => 301)) called at [/var/www/vhosts/danielwahl.lu/wp.crla.danielwahl.lu/wp-includes/plugin.php:212]
#2  apply_filters(wp_redirect, https://wp.crla.danielwahl.lu/, 301) called at [/var/www/vhosts/danielwahl.lu/wp.crla.danielwahl.lu/wp-includes/pluggable.php:1290]
#3  wp_redirect(https://wp.crla.danielwahl.lu/, 301) called at [/var/www/vhosts/danielwahl.lu/wp.crla.danielwahl.lu/wp-includes/canonical.php:799]
#4  redirect_canonical(http://wp.crla.danielwahl.lu/) called at [/var/www/vhosts/danielwahl.lu/wp.crla.danielwahl.lu/wp-includes/class-wp-hook.php:292]
#5  WP_Hook->apply_filters(, Array ([0] => )) called at [/var/www/vhosts/danielwahl.lu/wp.crla.danielwahl.lu/wp-includes/class-wp-hook.php:316]
#6  WP_Hook->do_action(Array ([0] => )) called at [/var/www/vhosts/danielwahl.lu/wp.crla.danielwahl.lu/wp-includes/plugin.php:484]
#7  do_action(template_redirect) called at [/var/www/vhosts/danielwahl.lu/wp.crla.danielwahl.lu/wp-includes/template-loader.php:13]
#8  require_once(/var/www/vhosts/danielwahl.lu/wp.crla.danielwahl.lu/wp-includes/template-loader.php) called at [phar:///usr/local/bin/wp/vendor/wp-cli/eval-command/src/EvalFile_Command.php(76) : eval()'d code:20]
#9  eval() called at [phar:///usr/local/bin/wp/vendor/wp-cli/eval-command/src/EvalFile_Command.php:76]
#10 EvalFile_Command::execute_eval(/var/www/vhosts/danielwahl.lu/wp.crla.danielwahl.lu/wp-cli-run-frontend.php, Array ()) called at [phar:///usr/local/bin/wp/vendor/wp-cli/eval-command/src/EvalFile_Command.php:39]
#11 EvalFile_Command->__invoke(Array (), Array ())
#12 call_user_func(Array ([0] => EvalFile_Command Object (),[1] => __invoke), Array ([0] => wp-cli-run-frontend.php), Array ()) called at [phar:///usr/local/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Dispatcher/CommandFactory.php:98]
#13 WP_CLI\Dispatcher\CommandFactory::WP_CLI\Dispatcher\{closure}(Array ([0] => wp-cli-run-frontend.php), Array ())
#14 call_user_func(Closure Object (), Array ([0] => wp-cli-run-frontend.php), Array ()) called at [phar:///usr/local/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Dispatcher/Subcommand.php:451]
#15 WP_CLI\Dispatcher\Subcommand->invoke(Array ([0] => wp-cli-run-frontend.php), Array (), Array ()) called at [phar:///usr/local/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php:371]
#16 WP_CLI\Runner->run_command(Array ([0] => eval-file,[1] => wp-cli-run-frontend.php), Array ()) called at [phar:///usr/local/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php:394]
#17 WP_CLI\Runner->run_command_and_exit() called at [phar:///usr/local/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php:87]
#18 WP_CLI\Runner->do_early_invoke(before_wp_load) called at [phar:///usr/local/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php:1102]
#19 WP_CLI\Runner->start() called at [phar:///usr/local/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Bootstrap/LaunchRunner.php:23]
#20 WP_CLI\Bootstrap\LaunchRunner->process(WP_CLI\Bootstrap\BootstrapState Object ([WP_CLI\Bootstrap\BootstrapStatestate] => Array ())) called at [phar:///usr/local/bin/wp/vendor/wp-cli/wp-cli/php/bootstrap.php:74]
#21 WP_CLI\bootstrap() called at [phar:///usr/local/bin/wp/vendor/wp-cli/wp-cli/php/wp-cli.php:27]
#22 include(phar:///usr/local/bin/wp/vendor/wp-cli/wp-cli/php/wp-cli.php) called at [phar:///usr/local/bin/wp/php/boot-phar.php:11]
#23 include(phar:///usr/local/bin/wp/php/boot-phar.php) called at [/usr/local/bin/wp:4]
szepeviktor commented 3 years ago

An s is missing from the protocol: https://wp.crla.danielwahl.lu/ is the canonical URL.

DanielWahl commented 3 years ago

True, the s was missing.

Now the errors also were more clear and I found the issue.

public function add_to_twig( $twig ) {
        $twig->addExtension( new Twig\Extension\StringLoaderExtension() );
        $twig->addFilter( new Twig\TwigFilter( 'myfoo', array( $this, 'myfoo' ) ) );
        return $twig;
    }

new Twig\Extension and new Twig\TwigFilterwere a \ missing in the new class-file. So adding

new \Twig\Extension...
new \Twig\TwigFilter...

Fixed the issue.

@szepeviktor Thanks very much for your help.

szepeviktor commented 3 years ago

You're welcome.

Please consider importing used classes with use Twig\Extension\StringLoaderExtension;