1

When I create a constant in class "Product", then I can use this constant in other classes with Product::id and all is well. :-)

<?php
class Product
{ 
    const id = "4325536";
}
...

But when I try to get a value from URL into const, nothing works:

<?php
class Product
{ 
    const id = $_GET['product'];    
}
...

How can I get values from URL into Object?

user2830595
  • 47
  • 1
  • 4
  • Use ```define(name,value)``` in ```constructor```. http://php.net/manual/en/function.define.php – Mohammad Zare Moghadam Aug 05 '16 at 10:16
  • Are you sure a constant is what you are looking for? It seems more like you want a static class member http://stackoverflow.com/questions/3818333/how-do-i-access-static-member-of-a-class – rypskar Aug 05 '16 at 10:45

3 Answers3

2

You can't. Value for the class constant needs to be determined at compile/parse time. Values passed in the request can only be evaluated at runtime.

See the docs for more details on how to use class constants in PHP: http://php.net/manual/pl/language.oop5.constants.php

jedrzej.kurylo
  • 39,591
  • 9
  • 98
  • 107
  • I'm staggered. That would mean, that OOP ist completely useless. Are there any other ways to get values from URL into an object? – user2830595 Aug 05 '16 at 10:10
  • You can always access request variables at runtime – jedrzej.kurylo Aug 05 '16 at 10:18
  • @jedrzej.kurylom, we can achieve by define global constant outside of the class and initialize constant of class by global constant – Haresh Vidja Aug 05 '16 at 13:12
  • @jedrzej.kurylo, Yes I know its something like a hack or patch in code, but sometime we need to do some tricks to achieve requirement, In all applications and frameworks they have tried to achieve good practice of code upto 99%, but for achieve something like this they have used some tricks inside of code, we are doing this kind of tricks frequently when we are using a third party modules. I hope you are agree with me. – Haresh Vidja Aug 08 '16 at 05:24
  • TBH I cannot imagine any business requirement that would require anyone to do what OP wants to do, especially that business requirements rarely impact the way code is structured on such a low level. There are better ways of accessing request-specific data than putting them in class constants – jedrzej.kurylo Aug 08 '16 at 07:34
1

You can do something like this

<?php

define('ID',$_GET['product']); 

class Product
{ 
    const id = ID;    
}

I am not sure but, refer for this. http://php.net/manual/en/language.oop5.constants.php Example #3

Haresh Vidja
  • 8,340
  • 3
  • 25
  • 42
  • This does not work. If you would use `const ID=4325536;` all is well, but not `const ID= $_GET['product'];` – user2830595 Aug 05 '16 at 11:08
  • GREAT!!!! - Thank you very much!! Best answer. (Sorry, due to my low reputation, I cannot upvote your answer). – user2830595 Aug 05 '16 at 11:19
  • Glad to hear about its working ... I have upvoted your question so now you have right to give up vote :).. don't forget to mark as right... thanks anyway – Haresh Vidja Aug 05 '16 at 11:22
0
class Y
{
    public function __construct($var)
    {
        define("X",$var);
    }
}

new Y($_GET['x']);
  • 1
    @Mohmad Zare, here x is not class constants, its a global constant :) – Haresh Vidja Aug 05 '16 at 11:16
  • While this code snippet may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, as this reduces the readability of both the code and the explanations! – Blue Aug 05 '16 at 13:52