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? Link to comment https://forums.phpfreaks.com/topic/286109-public-variables-in-php-classes-_getname/ 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']; } } Link to comment https://forums.phpfreaks.com/topic/286109-public-variables-in-php-classes-_getname/#findComment-1468477 Share on other sites More sharing options...
iNealec Posted February 11, 2014 Author Share Posted February 11, 2014 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 Link to comment https://forums.phpfreaks.com/topic/286109-public-variables-in-php-classes-_getname/#findComment-1468487 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. Link to comment https://forums.phpfreaks.com/topic/286109-public-variables-in-php-classes-_getname/#findComment-1468490 Share on other sites More sharing options...
iNealec Posted February 11, 2014 Author Share Posted February 11, 2014 $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"; } Link to comment https://forums.phpfreaks.com/topic/286109-public-variables-in-php-classes-_getname/#findComment-1468493 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. Link to comment https://forums.phpfreaks.com/topic/286109-public-variables-in-php-classes-_getname/#findComment-1468512 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. Link to comment https://forums.phpfreaks.com/topic/286109-public-variables-in-php-classes-_getname/#findComment-1468521 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.