ComGuar Posted October 4, 2011 Share Posted October 4, 2011 this code doesn't work, why? define("DIR", "../../"); final class MyClass { private static $PATH = DIR . '../directory/'; } Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted October 4, 2011 Share Posted October 4, 2011 Class property declarations can only be a literal (string, array, int, etc..) or a constant, not an expression. Your line is concatenating a string and a constant which is an expression. Quote Link to comment Share on other sites More sharing options...
ComGuar Posted October 5, 2011 Author Share Posted October 5, 2011 this code doesn't work too define("DIR", "../../"); $dir = DIR; final class MyClass { public $dir; private static $PATH = $dir . '../directory/'; } Quote Link to comment Share on other sites More sharing options...
BizLab Posted October 5, 2011 Share Posted October 5, 2011 Humm... first, I would probably move my constants to a file outside the class and include them in the script in order to keep all constants in one location. You have a high-level of restriction set on the sample code.. final classes are the end of the line, and can't be overridden or extended. Maybe not the best choice, but it all depends on your implementation The use of OOP is to create widely reusable code that is usable in various projects.. declaring a filepath inside a class is not well-advised. i suggest : class myClass{ // properties protected $_directory; // construct (used to pass attributes to the class) public function __construct($directory){ $this->_directory = $directory } } This way, the directory used by this class can be called at runtime using // instantiate the object $object = new myClass(MY_DIR_CONSTANT); And then you can work on that. I'd recommend grabbing an OOP book if none of this makes any sense. Let me know if you have any problems Quote Link to comment Share on other sites More sharing options...
ComGuar Posted October 7, 2011 Author Share Posted October 7, 2011 no, this is not solution.. I must use: define("DIR", "../../"); so this is not an option.. Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted October 7, 2011 Share Posted October 7, 2011 define("DIR", "../../"); define("PATH", DIR . "../directory/"); final class MyClass { private static $PATH = PATH; } Quote Link to comment Share on other sites More sharing options...
ComGuar Posted October 8, 2011 Author Share Posted October 8, 2011 Thanks mate, this is working great! 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.