syntax error, unexpected '(', expecting ',' or ';'

Permalink
I'm trying to get either a site name or a logged in user name or admin's email address in my own package's controller and every time I get this error:

syntax error, unexpected '(', expecting ',' or ';'

But I can't find anything wrong with the code, it's a one-to-one copy from the systems blocks.
use UserInfo;       // Also tried Concrete\Core\User\UserInfo
use Config;          // Also tried Concrete\Core\Config
...
public $site_name = Config::get('concrete.site');
or this:
$adminUserInfo=UserInfo::getByID(USER_SUPER_ID);
$this->email_to = $adminUserInfo->getUserEmail();
or this:
$u = new User();
$ui = UserInfo::getByID($u->getUserID());
$this->email_to = $ui->getUserEmail();

If I use the same code in the view.php, it all works fine without errors.

What's wrong with the controller?

Thank you.

linuxoid
 
KelvinSmith replied on at Permalink Reply
KelvinSmith
Hi,
Which PHP version are you using?
linuxoid replied on at Permalink Reply
linuxoid
Hi,
5.6.9

Could it be something to do with namespaces?

I'm using namespace Concrete\Package\MyPackage\Block\MyPackage;
ramonleenders replied on at Permalink Reply
ramonleenders
Works just fine with me. Write these use statements (look at the last one, that's the one you're missing most likely):

use Config;
use UserInfo;
use User;


And this code:

$site_name = Config::get('concrete.site');
        var_dump($site_name);
        $adminUserInfo=UserInfo::getByID(USER_SUPER_ID);
        $this->email_to = $adminUserInfo->getUserEmail();
        var_dump($adminUserInfo);
        var_dump($this->email_to);
        $u = new User();
        $ui = UserInfo::getByID($u->getUserID());
        $this->email_to = $ui->getUserEmail();
        var_dump($u);
        var_dump($ui);
        var_dump($this->email_to);
        exit;


You see values being dumped? If so, remove all var dumps and exits and use to your liking! :) I do assume you're on Concrete5 5.7 as this will not work in 5.6.
linuxoid replied on at Permalink Reply
linuxoid
No, nothing works, it still gives me errors.

I made a simple test:

controller.php:
<?php 
namespace Concrete\Package\MyPackage\Block\MyPackage;
use \Concrete\Core\Block\BlockController;
use Config;
class Controller extends BlockController {
   public $site = Config::get('concrete.site');
   public function view()
   {
      $this->set('site', $this->site);
   }
}

view.php:
<?php 
defined('C5_EXECUTE') or die(_("Access Denied."));
echo $site;
?>

Still the same error:
Whoops \ Exception \ ErrorException (E_PARSE)
syntax error, unexpected '(', expecting ',' or ';' 
/­srv/­www/­htdocs/­test/­packages/­my_package/­blocks/­my_package/­controller.php12

If I change the controller to
namespace Concrete\Package\MyPackage\Block\MyPackage;
use \Concrete\Core\Block\BlockController;
use Config;
class Controller extends BlockController {
   public function view()
   {
      $this->set('site', "My Site");
   }

It works fine. Same problem with the user and email. Same error if I use use 'namespace \Concrete\Core\Config;' How come? How do you get all that stuff in controllers of your own packages?
linuxoid replied on at Permalink Reply
linuxoid
O! If I define the variable in the view function, it works:
<?php 
namespace Concrete\Package\MyPackage\Block\MyPackage;
use \Concrete\Core\Block\BlockController;
use Config;
class Controller extends BlockController {
   public function view()
   {
      $site = Config::get('concrete.site');
      $this->set('site', $site);
   }
}

But how do I get it to work within my class, not just within the view function?
ramonleenders replied on at Permalink Reply
ramonleenders
OK, I did not know you were trying to make a call to the Config class upon defining the variable in the class itself (or just read through it). That's something you can not do. I'm not sure how I can explain it, but you simply can't. You can't define a variable at the top of the class either if you want to call a normal function (you're calling a function of another class now, the config class). They all have to be plain PHP code (array, boolean, integer etc.). If you DO want to declare it, declare it and make it an empty string (or null). Then fill it on the on_start function for example -->

public function on_start(){
    $this->site = Config::get('concrete.site');
    $this->set('site', $this->site)
}


You can also on set the site in your view function if you'd like, as you probably do not need this variable in every function of your controller?

Does this make sense to you? :)
linuxoid replied on at Permalink Reply
linuxoid
Sorry, doesn't make sense. But I'll just trust you, cos that's how it works. Maybe it's something to do with PHP OOP?

Anyway, I'm setting all my variables in the view function now and it works.

Thank you.