Welcome Guest, Not a member yet? Register   Sign In
How to fix this variable assignment?
#1

[eluser]whygod[/eluser]
Hi guys

How to fix this codes below,
Code:
public $main_page = base_url() . 'jaz/';

As you can see the codes above will produce an error below,
Code:
Parse error: syntax error, unexpected '(', expecting ',' or ';' in /home/site_removed/public_html/admins9b/application/controllers/jaz.php on line 10

So how do I assign the value/string above the proper way?

Thanks in advanced.
#2

[eluser]whygod[/eluser]
These are the complete codes,
Code:
<?php
if (! defined('BASEPATH')) exit('No direct script access allowed');

class Jaz extends MY_Controller
{
public $data = array(
                   '' => '',
    );
    
public $main_page = $base_url() . 'jaz/';
  

    function __construct()
    {
        parent::__construct();
        $this->load->helper('general');
        $this->load->model('users_model');
  $this->load->library('email');
     }

}

Why it's producing error?
#3

[eluser]jmadsen[/eluser]
your two sets of posted code are different.

the "real" one:

Code:
public $main_page = $base_url() . 'jaz/';

is using the function base_url() as a variable (i.e., __$__base_url() )


after that, you should declare:

public $main_page;

then in the __construct() function set it as:

$this->main_page = base_url() . 'jaz/';


Before you call the construct of the controller, you don't have a reference to the $CI object, and so the url helper, etc are not loaded yet
#4

[eluser]PhilTem[/eluser]
Nope, that's not the problem. PHP allows just a variable type declarations when defining attributes. You cannot assign variables directly.

This does not work
Code:
class Test {
  public $thingy = base_url();
  public $thongy = array('base' => 'here);
}

Howerver, such things work
Code:
class Test {
  public $null = NULL;
  public $array = array();
  public $false = FALSE;
  public $true = TRUE;
  public $string = "";
  public $empty = '';
}

Any other value assignment (as you do with public $item = base_url() must be done in the class constructor.

That's how PHP rolls!




Theme © iAndrew 2016 - Forum software by © MyBB