We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Phalcon -> Fatal error: Access to undeclared static property: Phalcon\Di::$_default …public/index.php on line 20

I've installed Phalcon on my server Centos 7 with PHP-FPM 5.6

I'm following the basic instructions at https://docs.phalcon.io/en/latest/reference/tutorial.html and get stuck at the beginning:

Sometimes it displays the correct "Hello!" message, but I've refreshed the page several times and most of the time I get the following error:

Fatal error: Access to undeclared static property: Phalcon\Di::$_default in /var/www/vhosts/playerm8.com/httpdocs/public/index.php on line 20

My public/index.php looks like this:

<?php

use Phalcon\Loader;
use Phalcon\Mvc\View;
use Phalcon\Mvc\Application;
use Phalcon\Di\FactoryDefault;
use Phalcon\Mvc\Url as UrlProvider;
use Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter;

try {

    // Register an autoloader
    $loader = new Loader();
    $loader->registerDirs(array(
        '../app/controllers/',
        '../app/models/'
    ))->register();

    // Create a DI
    $di = new FactoryDefault()
;
    // Setup the view component
    $di->set('view', function () {
        $view = new View();
        $view->setViewsDir('../app/views/');
        return $view;
    });

    // Setup a base URI so that all generated URIs include the "tutorial" folder
    $di->set('url', function () {
        $url = new UrlProvider();
        $url->setBaseUri('/');
        return $url;
    });

    // Handle the request
    $application = new Application($di);

    echo $application->handle()->getContent();

} catch (\Exception $e) {
     echo "Exception: ", $e->getMessage();
}

And my apps/controllers/IndexController.php looks like this:

<?php

use Phalcon\Mvc\Controller;

class IndexController extends Controller
{

    public function indexAction()
    {
        echo "<h1>Hello!</h1>";
    }
}

Any help would be appreciated

edited Jul '16

On your line 20, you have semicolom in next line. Even though that should not be an issue in PHP, try to put it in a same row as your code.

    // Create a DI
    $di = new FactoryDefault();

Also remove BOM from your PHP file (if any), and make sure you don't use any CR/LF (carriage return) but only LF for new lines.



2.7k
Accepted
answer

Thanks Stamster, sorry that was a mistake when copying and pasting from my code to this forum, the semicolon was in the same row in my code and no BOM or any format errors in the code.

I believe the issue was something to do with Plesk. I've removed Plesk from my server and run Apache and it's working properly now. Actually the code is correct.

On your line 20, you have semicolom in next line. Even though that should not be an issue in PHP, try to put it in a same row as your code.

  // Create a DI
   $di = new FactoryDefault();

Also remove BOM from your PHP file (if any), and make sure you don't use any CR/LF (carriage return) but only LF for new lines.

Yeah, that's why I said Even though that should not be an issue in PHP, try to put it in a same row as your code.

Plesk, ISPConfig etc. are just a hell and should be avoided at all costs in an enterprise deployed applications.

In any case, 'm glad that you solved it.

The best is to use Control Panel which DON'T provide any server/php handler itself, which is just working on config files from nginx/apache without touching apache/nginx.