MeTitus Posted April 11, 2008 Share Posted April 11, 2008 Hi guys, I am getting the following: "unexpected T_VARIABLE " error in this line: public static $WEBSERVICE_CONF_FILE = $_SERVER['HTTP_HOST']; but if I do this: public static $WEBSERVICE_CONF_FILE = ""; I don't get the error any more. I am not missing any curl brackets or semi colons either... Any ideas? Thanks in advance, Marco Quote Link to comment https://forums.phpfreaks.com/topic/100629-_serverhttp_host/ Share on other sites More sharing options...
Daniel0 Posted April 11, 2008 Share Posted April 11, 2008 I don't think you can initialize class properties like that. You could just set it in the constructor though. Quote Link to comment https://forums.phpfreaks.com/topic/100629-_serverhttp_host/#findComment-514640 Share on other sites More sharing options...
MeTitus Posted April 11, 2008 Author Share Posted April 11, 2008 But it works with the other members: public static $WEBSERVICE_CONF_FILE = $_SERVER['HTTP_HOST']; public static $QUERY_WORD = "webCall"; public static $SERVER = "EM417"; public static $CHARSET = "iso-8859-1"; public static $WEB_SERVICES_ROOT = "./webServicesEndPoints/"; public static $HTTP_GET = "GET"; public static $HTTP_POST = "POST"; public static $HTTP_PUT = "PUT"; public static $HTTP_DELETE = "DELETE"; $_SERVER['HTTP_HOST'] returns a string value, so why should it work? Quote Link to comment https://forums.phpfreaks.com/topic/100629-_serverhttp_host/#findComment-514779 Share on other sites More sharing options...
PFMaBiSmAd Posted April 11, 2008 Share Posted April 11, 2008 $_SERVER['HTTP_HOST'] is a variable, the other values are constants. Please read the manual to find out why you cannot use a variable in a declaration. Quote Link to comment https://forums.phpfreaks.com/topic/100629-_serverhttp_host/#findComment-514829 Share on other sites More sharing options...
MeTitus Posted April 11, 2008 Author Share Posted April 11, 2008 It simply makes no sense at all. In all of the other languages I use: Java, c# and c I can do that just fine, I can't really see where the problem is. And the the members are no constants, they are just static members, class and not instance values. Marco Quote Link to comment https://forums.phpfreaks.com/topic/100629-_serverhttp_host/#findComment-515130 Share on other sites More sharing options...
Daniel0 Posted April 11, 2008 Share Posted April 11, 2008 My guess is that the value is being set at compile-time and therefore the variables are not set yet. The default value must be a constant expression' date=' not (for example) a variable, a class member or a function call.[/quote'] Quote Link to comment https://forums.phpfreaks.com/topic/100629-_serverhttp_host/#findComment-515162 Share on other sites More sharing options...
discomatt Posted April 11, 2008 Share Posted April 11, 2008 Why not just declare them, create a static function that gives them value, and call the static function immediately after ending the class declaration? Quote Link to comment https://forums.phpfreaks.com/topic/100629-_serverhttp_host/#findComment-515171 Share on other sites More sharing options...
MeTitus Posted April 11, 2008 Author Share Posted April 11, 2008 Thanks guys for the replies Yes there are lots of workarounds for this, but it just shows yet another php poor design. Instance methods are initialized before any class constructor is invoked and the same goes to the static methods. I can do that in Java and it would work just fine, as an e.g.: public class Main { private static final int iValue1 = 1; private static final int iValue2 = Main.iValue1==1?2:1;; public static void main(String[] args) { System.out.println(Main.iValue2); } } This would work without any problem. $_SERVER['HTTP_HOST'] evaluates to a value, whether that generates a runtime error is another problem but in this specific case is not that. I am using php for a class in my computer master, and I've been finding some many limitations in this language so far, which makes me wonder why is it so widely used, I can't realy undertand. Anyway, I will have to get yet another work around. Once again thanks, MeTitus Quote Link to comment https://forums.phpfreaks.com/topic/100629-_serverhttp_host/#findComment-515237 Share on other sites More sharing options...
discomatt Posted April 11, 2008 Share Posted April 11, 2008 It may not work the way you want it to, but it works the way it's supposed to. Quote Link to comment https://forums.phpfreaks.com/topic/100629-_serverhttp_host/#findComment-515242 Share on other sites More sharing options...
MeTitus Posted April 11, 2008 Author Share Posted April 11, 2008 Hi discomatt, Its not like that, because when any programming language support OOP and in php that started with version 5, they have to follow a couple of rules, and php fails to follow too many. Its not the way I want its the way it should. This is something very basic that every OOP language I know of support, why wouldn't php when they say it supports OOP. Maybe php supports OOP- instead. Something I have seen for the past 3 months while doing this coursework is that people that started with earlier versions of php and then started using php 5 make lots of wrong assumption about OOP and the way it should work. Fortunatelly I started with OOP back in 98 so I know that this is a design flaw rather than a personal taste. Don't take me wrong as I am not trying to start nothing here. I do undertand that php fits many programmers and companies needs, just shooting off something I see as not being well done. Marco Quote Link to comment https://forums.phpfreaks.com/topic/100629-_serverhttp_host/#findComment-515251 Share on other sites More sharing options...
PFMaBiSmAd Posted April 11, 2008 Share Posted April 11, 2008 In this example, php works the way it does because it is a parsed/tokenized/interpreted language. Variable declarations cannot contain a value that is determined at runtime. The other languages you mention are multi-pass compilers. If php was made into a more complicated multi-pass parser, then it would allow you to use a variable as a value in a declaration. But then it would be slower and not usable as a web based server side scripting language. You have to trade off features or performance sometimes. You have to be flexible and accept the limitations and rules of a language, or don't use that language. You are trying to generalize that all OOP in all programming languages are the same. It is not. Quote Link to comment https://forums.phpfreaks.com/topic/100629-_serverhttp_host/#findComment-515272 Share on other sites More sharing options...
MeTitus Posted April 11, 2008 Author Share Posted April 11, 2008 Hi PFMaBiSmAd, I would not use it, but unfortunately I've too if I want to finish the coursework. Of course I don't want to generalize the OOP concept but there are a few things that every OOP language should have, and saying that php does not have this because it is an interpreted language is not a valid argument here, python and ruby are and support that, its not even support because its not a special feature... In relation to the trade off, java for eg is by far faster than php, we can't compare this here, php by being interpreted at run-time is much slower, and if I trully wanted speed I would C CGIs. The only point I can see, is that php allows any non deep technical guy to do something. Just look at the mysql library, it was so badly developped that they had to create a new one, if it was a well structure language that would not happen, they could fix all the problems without affecting exixting users, and that is just one eg, but the funniest one I found was the one related to the exception handling... even if you use try...catch most of the exceptions are not catched... how can this happen?? Marco Quote Link to comment https://forums.phpfreaks.com/topic/100629-_serverhttp_host/#findComment-515275 Share on other sites More sharing options...
Daniel0 Posted April 12, 2008 Share Posted April 12, 2008 [...] but the funniest one I found was the one related to the exception handling... even if you use try...catch most of the exceptions are not catched... how can this happen?? Post an example of an exception which is not caught by that... I'd like to see that. Quote Link to comment https://forums.phpfreaks.com/topic/100629-_serverhttp_host/#findComment-515347 Share on other sites More sharing options...
MeTitus Posted April 13, 2008 Author Share Posted April 13, 2008 That one is easy... http://bugs.php.net/bug.php?id=27283&edit=2 Yes of course this one is a bug lol. What about these: error_reporting(0); try { $addresses = array('[email protected]', '[email protected]'); for($i = 0; $i < count($addresses) + 1; $i++) { $value = $addresses[$i]; } } catch(Exception $ex) { echo ("I was catched - 1"); } try { $objXml = simplexml_load_file(""); } catch(Exception $ex) { echo ("I was catched - 2"); } try { echo inverse(5) . "\n"; echo inverse(0) . "\n"; } catch (Exception $e) { echo ("I was catched - 3"); } I should see all the echos but I see none... MeTitus Quote Link to comment https://forums.phpfreaks.com/topic/100629-_serverhttp_host/#findComment-516362 Share on other sites More sharing options...
Daniel0 Posted April 13, 2008 Share Posted April 13, 2008 That's because nothing within the try blocks could possibly throw exceptions... The bug you linked to has been fixed. Quote Link to comment https://forums.phpfreaks.com/topic/100629-_serverhttp_host/#findComment-516367 Share on other sites More sharing options...
MeTitus Posted April 14, 2008 Author Share Posted April 14, 2008 Did you test it at least?? This is the exceptions you get: Notice: Undefined offset: 2 Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity Fatal error: Call to undefined function inverse() MeTitus Quote Link to comment https://forums.phpfreaks.com/topic/100629-_serverhttp_host/#findComment-516379 Share on other sites More sharing options...
Daniel0 Posted April 14, 2008 Share Posted April 14, 2008 Did you test it at least?? No need. I already knew the result. This is the exceptions you get: Notice: Undefined offset: 2 Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity Fatal error: Call to undefined function inverse() They're not exceptions, but errors... An uncaught exception would look something like this: Fatal error: Uncaught exception 'Exception' with message 'test' in C:\wamp\www\test.php:2 Stack trace: #0 {main} thrown in C:\wamp\www\test.php on line 2 Quote Link to comment https://forums.phpfreaks.com/topic/100629-_serverhttp_host/#findComment-516488 Share on other sites More sharing options...
MeTitus Posted April 14, 2008 Author Share Posted April 14, 2008 You must be jocking right?? This one you are right, it would be like a compilation error, because the function doens't exist. The other ones are exceptions, call me errors if you want, but it just the same. The truth here is that there are errors being generated and they are not being catch. BTW Exceptions aren't just the ones thrown with throw statements. MeTitus Quote Link to comment https://forums.phpfreaks.com/topic/100629-_serverhttp_host/#findComment-517167 Share on other sites More sharing options...
Daniel0 Posted April 15, 2008 Share Posted April 15, 2008 BTW Exceptions aren't just the ones thrown with throw statements. In PHP it is. In the code you provided there are thrown no exceptions whatsoever. Try to read the manual again and you'll find I'm right. I see you do Java as well, and yeah, with Java, exceptions and run-time errors are the same thing, but that doesn't mean every single language does that. In PHP there is a distinction between errors and exceptions. Quote Link to comment https://forums.phpfreaks.com/topic/100629-_serverhttp_host/#findComment-517420 Share on other sites More sharing options...
MeTitus Posted April 22, 2008 Author Share Posted April 22, 2008 Maybe the reason behind this is due to the fact that PHP wasn't supposed to have exception handling in the first place MeTitus Quote Link to comment https://forums.phpfreaks.com/topic/100629-_serverhttp_host/#findComment-524523 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.