iNealec Posted February 11, 2014 Share Posted February 11, 2014 I am working with a php class which contains public variables like so: public $var = "sometext"; <-- This works But when I try to assign an $_GET['value'] to that variable, dreamweaver is saying there is a syntax error. public $var = $_GET['value']; <-- this doesn't work Is there a way around this problem? Quote Link to comment Share on other sites More sharing options...
.josh Posted February 11, 2014 Share Posted February 11, 2014 Property declarations can only be declared with a constant value. You can assign it that value by doing it in your __construct method. class Foo { public $var; function __construct() { $this->var = $_GET['value']; } } Quote Link to comment Share on other sites More sharing options...
iNealec Posted February 11, 2014 Author Share Posted February 11, 2014 (edited) Doesn't seem to be working, there's no more syntax errors but it does no get the value of $_GET. Trying to use it like this: $filename = $_GET['value']."-file.txt"; But the file just comes out: -file.txt Edited February 11, 2014 by iNealec Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted February 11, 2014 Share Posted February 11, 2014 It may help if you post the code where the GET variable is being used within the class. To make the code (and your post) easier to follow, please surround the code with tags. Quote Link to comment Share on other sites More sharing options...
iNealec Posted February 11, 2014 Author Share Posted February 11, 2014 (edited) $createuserfile = $_GET['userID']."-file.txt"; echo $_GET['userID']; <-- This works, but the file created below gets named -file.txt $ourFileHandle = fopen($createuserfile, 'w') or die("can't open file"); fclose($ourFileHandle); $user = new Some_Class(); class Some_Class { public $userfile; function __construct() { $this->userfile = $_GET['userID']."-file.txt"; } Edited February 11, 2014 by iNealec Quote Link to comment Share on other sites More sharing options...
.josh Posted February 11, 2014 Share Posted February 11, 2014 <?php $createuserfile = $_GET['userID']."-file.txt"; echo $_GET['userID']; $user = new Some_Class(); echo "<br/>"; echo "prop: ".$user->userfile; class Some_Class { public $userfile; function __construct() { $this->userfile = $_GET['userID']."-file.txt"; } } And then if i go to script.php?userID=foobar, it outputs: foobar prop: foobar-file.txt So there's something else going wrong in your code, wherever you are actually trying to use $this->userfile, that you're not showing. Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted February 11, 2014 Share Posted February 11, 2014 A slight tweak to Some_Class (I'll rename it SomeClass): class SomeClass { public $userFile; public function __construct($file) { $this->userFile = $file; } } $myFile = new SomeClass($_GET['userID'] . "-file.txt"); I like it better that way because otherwise the class is dependent on $__GET['userID'] existing, which isn't good. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.