Variable Issues..........[Help] - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Variable Issues..........[Help] (/showthread.php?tid=29523) Pages:
1
2
|
Variable Issues..........[Help] - El Forum - 04-12-2010 [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 Variable Issues..........[Help] - El Forum - 04-13-2010 [eluser]mddd[/eluser] When you call a Class variable, you have to use $this->$birthdayData. Not just $birthdayData. That will have no value. Variable Issues..........[Help] - El Forum - 04-13-2010 [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 Variable Issues..........[Help] - El Forum - 04-13-2010 [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 Later on, when using these properties you have to use $this->currentYear and $this->birthdayData. Variable Issues..........[Help] - El Forum - 04-13-2010 [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 Oh, my god... Why....... Oh, and btw my file is located in "..\application\controllers\TXTU\reg.php" Variable Issues..........[Help] - El Forum - 04-13-2010 [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 Variable Issues..........[Help] - El Forum - 04-13-2010 [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 ';' Variable Issues..........[Help] - El Forum - 04-13-2010 [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 Variable Issues..........[Help] - El Forum - 04-13-2010 [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 edit: mddd was too quick ;-) Variable Issues..........[Help] - El Forum - 04-13-2010 [eluser]alphane[/eluser] Try declaring the variables without assigning any values. Then assign your values within your constructor. Like so: Code: <?php I think i've done similar in the past - not 100% edit **damn too slow** |