Jump to content

$_SERVER['HTTP_HOST']


MeTitus

Recommended Posts

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

[...] 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.

Link to comment
Share on other sites

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('spam@cyberpromo.net', 'abuse@example.com');

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.