Welcome Guest, Not a member yet? Register   Sign In
Variable Issues..........[Help]
#1

[eluser]Kanra[/eluser]
Hi, everyone

I haven't played with php for a longtime and now having a little issue hope anyone can help.

Here is part of my code, how can I create $currentYear and $birthdayData so that both my index() & reg_Result() functions can use them?

It keeps saying

Parse error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION




Code:
<?php
class Reg extends Controller
{
        //problem code
        $currentYear = date("Y");
        $birthdayData = array("year"=>$currentYear, "i"=>0, "firstThru"=>TRUE);
        //problem code

    function Reg()
    {    
        //Constructor
        parent::Controller();
        $this->load->helper(array('form', 'url', 'date'));
        $this->load->library(array('form_validation', 'encrypt'));
    }
    
    function index()
    {
        //Load the registration view
        $this->load->view('TXTU/regView', $birthdayData);
    }

    
    function reg_Result()
    {
        //set rules for my form validation
        $this->set_regform_rules();
        
        if ($this->form_validation->run() == FALSE)
        {                        
            $this->load->view('TXTU/regView', $birthdayData);
        }
        else
        {
            $this->save_reg_data();
            $this->load->view('TXTU/regSuccess');
        }
    }
?>
#2

[eluser]mddd[/eluser]
When you call a Class variable, you have to use $this->$birthdayData. Not just $birthdayData. That will have no value.
#3

[eluser]Kanra[/eluser]
[quote author="mddd" date="1271157418"]When you call a Class variable, you have to use $this->$birthdayData. Not just $birthdayData. That will have no value.[/quote]

Thank you very much for your reply~

My problem is that I can not even assign value to these two variables
as soon as i run the code, it tells me following message

Parse error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION in ......application\controllers\TXTU\reg.php on line 4

I don't know why.... I tried to add $this infront of the declaration
e.g. $this->$currentYear = date("Y") - 5;

and it still complains~~

Code:
<?php
class Reg extends Controller
{
    $currentYear = date("Y") - 5;
    $birthdayData = array("year"=>$currentYear, "i"=>0, "firstThru"=>TRUE);

    function Reg()
    {    
        //Constructor
        parent::Controller();
        $this->load->helper(array('form', 'url', 'date'));
        $this->load->library(array('form_validation', 'encrypt'));
    }
}
?>
#4

[eluser]mddd[/eluser]
It was so simple I missed it.. You need to put 'var' or declare a type for your class properties. So:
Code:
class Reg extends Controller
{
    var $currentYear = date('Y');
    var $birthdayData = ...

Later on, when using these properties you have to use $this->currentYear and $this->birthdayData.
#5

[eluser]Kanra[/eluser]
Now i'm really confused....

When I have an echo statement in between the declaration it says
Parse error: syntax error, unexpected T_ECHO, expecting T_FUNCTION

Code:
class Reg extends Controller
{
        //this has no problem
    var $currentYear1=2010;
        //this gives me Parse error: syntax error, unexpected T_ECHO, expecting T_FUNCTION
    echo "debug 1";
        //this gives me syntax error, unexpected '(', expecting ',' or ';'
    var $currentYear2=date('Y');
}

Oh, my god... Why.......

Oh, and btw my file is located in "..\application\controllers\TXTU\reg.php"
#6

[eluser]mddd[/eluser]
Please read the PHP manual about Objects and Classes.

You cannot have anything other than methods and properties in a Class.
That means that you can't put a command like echo in the class. It has to be inside a function of the class.

Code:
class MyClass
{
    // here, you can only declare variables
    var $MyVar = 'some_value';

    function MyFunction()
    {
      // here you can do stuff
    }
}
#7

[eluser]Kanra[/eluser]
Oh, yes that's the OO principles...

But I just can not make this line work ....

var $currentyear = date("Y");

It's seems sooo simple........BUT it keeps telling me

Parse error: syntax error, unexpected '(', expecting ',' or ';'
#8

[eluser]mddd[/eluser]
I'm sorry, I should have included that too. You can only set a property to a constant value; something that is independent of external information.
So you cannot use the function date() in there. The solution is to set this information in the constructor of your controller.
Code:
class MyClass
{
var $currentYear = 0;
var $birthdayData = array();

function __construct()
{
$this->currentYear = date('Y');
$this->birtdayData = array("year"=>$this->currentYear, "i"=>0, "firstThru"=>TRUE);
}
}
#9

[eluser]n0xie[/eluser]
You can't call a function or a language construct when you declare a class property (if we talk about variables belonging to a class, we talk about class properties to avoid the confusion you have right now.)

If you want to set a class property using something like 'date' you will have to do it from within the context of a class method (we talk about class methods to avoid confusion to normal 'functions'.)

So what does that mean:
Code:
// PHP4
class MyClass {

  var $currentyear;
  var $MyVar = 'some_value';

  function MyClass()
  {
    $this->currentyear = date('Y');
  }

  function MyFunction()
  {
    echo $this->MyVar;
    echo $this->currentyear;
  }
}

// PHP 5
class MyClass {

  public $currentyear;
  public $MyVar = 'some_value';

  function __construct()
  {
    $this->currentyear = date('Y');
  }

  function MyFunction()
  {
    echo $this->MyVar;
    echo $this->currentyear;
  }
}

edit: mddd was too quick ;-)
#10

[eluser]alphane[/eluser]
Try declaring the variables without assigning any values. Then assign your values within your constructor. Like so:

Code:
<?php
class Reg extends Controller
{
    //declare vars
    var $currentYear;
    var $birthdayData;

    function Reg()
    {    
        //Constructor
        parent::Controller();
        //assign values
        $this->birthdayData = array("year"=>$currentYear, "i"=>0, "firstThru"=>TRUE);
        $this->currentYear = date("Y");
    }

I think i've done similar in the past - not 100%

edit **damn too slow**




Theme © iAndrew 2016 - Forum software by © MyBB