0

Is there anyway to make an variable to protected and can't overwrite to php ?

p.s: Not in class or define.

For example;

<?php
$var ="something";
$var ="test";
echo $var;
?>

I want this code return "something" not "test".

Do you have any idea about this ?

René Höhle
  • 26,716
  • 22
  • 73
  • 82
merdincz
  • 427
  • 4
  • 16
  • 1
    no, this can only be done in a class, could you tell us why you don't want to use a define? Is there any problem for you when using a define? – Kevin Aug 08 '13 at 10:54
  • 1
    No, and there is no actual need to do this. Just use a constant instead. – Jon Aug 08 '13 at 10:56
  • 1
    What, you are looking for a "Variable" that should not change...??? !!! – Reshil Aug 08 '13 at 10:57
  • `Variable` that `varies`... – Rohan Kumar Aug 08 '13 at 10:59
  • Hi, i must use like "$var", so i can't use constants for special reason. – merdincz Aug 08 '13 at 11:03
  • 2
    the only way this would be useful is if you were "accidentally" changing the value, which means you should fix the real bug instead of slapping something like "protected" infront of the variable – bizzehdee Aug 08 '13 at 11:05
  • 1
    To summarize what everyone is trying to tell you: no, variables are variable, it is not possible, you're trying to solve the wrong problem. – deceze Aug 08 '13 at 11:07
  • A variable that doesn't change isn't a variable. If something has a value which can't change, then it's a **constant**. You define constants in php with `define` keyword. As deceze said, you're solving the wrong problem. Google XY problem. – N.B. Aug 08 '13 at 11:10
  • @merdincz A variable is, by definition, variable. If you want a variable which isn't variable, you want a constant. – Glitch Desire Aug 08 '13 at 11:23

3 Answers3

1

Technically you can use a helper function and the global (yuck) keyword, but classes/define is much better (but you'd still have to use a function and change all the $var = "something" lines to set_var("something")):

<?php
    set_var("something");
    set_var("test");

    var_dump($var); //string(9) "something"

    function set_var($newVar) {
        static $varIsSet;

        global $var;

        if (!$varIsSet) {
            $var = $newVar;
            $varIsSet = true;
        }
    }
?>

DEMO

h2ooooooo
  • 39,111
  • 8
  • 68
  • 102
  • Hi, thanks but when i use $var="test"; it's not working again. – merdincz Aug 08 '13 at 11:18
  • @merdincz No, because as I wrote, you need to use `set_var`. You **CANNOT** change how `$var=something` works unless you create your own PHP compiler. Change your logic and use constants instead of trying to work around a problem that is elsewhere. – h2ooooooo Aug 08 '13 at 11:19
  • i can't use set_var function for all variable. – merdincz Aug 08 '13 at 11:22
  • @merdincz Then listen to what everybody says instead and change your logic. **WHY** can you not use `define`? If you'd show us what the **actual** problem is we could help you come up with a work-around that isn't to change the PHP source code and create your own modified version. – h2ooooooo Aug 08 '13 at 11:26
1

That would go against the whole point of a variable

The whole purpose of a variable is that it is variable in value, it can change later in the program.

What can I use instead?

But you could use a constant:

// Works on PHP 5.3.0 and later
const SOME_CONSTANT = "Some value.";

OR

// Far more well known, works on older PHPs, does the same thing
define("SOME_CONSTANT", "Some value");

How do I use constants?

Then use it much like you would use a variable:

echo SOME_CONSTANT;

You can use a constant in nearly any way in which you can use a variable, just traditionally you'll use an upper-case name for the constant (this isn't enforced in PHP) and it wont start with a $ symbol (this is enforced)... oh and of course, you can't re-assign it.

Note: Because constants defined using the const keyword are defined at compile time, you CANNOT use this syntax inside loop structures, if statements, switch statements etc, and must use define() instead.

Glitch Desire
  • 14,632
  • 7
  • 43
  • 55
  • "*p.s: Not in class or define.*" – h2ooooooo Aug 08 '13 at 11:15
  • @h2ooooooo Right, and as explained, the answer is no. – Glitch Desire Aug 08 '13 at 11:17
  • I agree - it's really silly that OP doesn't want to use `define` or constants in general - I was only pointing it out in case you missed it. :-) – h2ooooooo Aug 08 '13 at 11:18
  • @h2ooooooo Well if he's using newer PHP (5.3 or later) he can use `const` outside of a class. – Glitch Desire Aug 08 '13 at 11:21
  • I learned something new today then - still - the only way it'd change anything for the OP is in case this is a job interview/homework question where you **can** use workarounds. (Just like "print numbers 0-100 without using a loop" - *psst - recursive functions*), but otherwise `const` should have the same "problem" for OP as `define` etc. My guess is that OP expects a magic word to place around the variable, so he doesn't need to fix the rest of the faulty code (basically creating code that's difficult to follow logically). – h2ooooooo Aug 08 '13 at 11:25
  • Hi, it's not interview or homework question. I bought a script several years ago from a company. config file is crypted in script and the company already closed so i can't reach any person on company. When i change my domain, script is not working because they use variable for domain. when i use get_defined_vars i can see $domain variable so if i modify and protect to change variable i guess i fix my problem. is it clear enough? – merdincz Aug 08 '13 at 11:35
  • @merdincz Very hacky solution, you'd probably be better off searching for everywhere the script loads the config file and assigning `$domain` there. That or going through `get_defined_vars` and rewriting config file in clear text. – Glitch Desire Aug 08 '13 at 11:58
  • @Lightning Dust, config file doesn't containing only variables probably containing function, class etc. so i think only way to fix my problem set variable protected. – merdincz Aug 08 '13 at 12:24
  • @merdincz You cannot set a variable in PHP as protected. Another option, `grep` the code for each instance of `$domain` and change it to `DOMAIN` then set a constant. – Glitch Desire Aug 08 '13 at 13:46
  • @Lightning Dust, i think i can't grep because in config file first setting var $domain ='domain' and check if($domain!=$dom) die; – merdincz Aug 08 '13 at 16:32
  • @merdincz If your script can decrypt config.xml, you can too. Work out how it does it. – Glitch Desire Aug 08 '13 at 16:37
  • @Lightning Dust, so how can i decrypt config.php ? i tried some function in php like reflection but not gonna succes. I'm searching how can sniff code but i can't found anything. Do you have any idea to decrypt codes ? – merdincz Aug 08 '13 at 16:51
  • @merdincz Search your code for where config.php is loaded. If there's no decryption function then `config.php` is not encrypted, it is obfuscated. If so, find a PHP de-obfuscator. – Glitch Desire Aug 08 '13 at 16:55
  • @Lightning Dust, codes are cyrpted with ioncube – merdincz Aug 08 '13 at 16:57
0

You could use functions to simulate that behavior:

function var() {
  return "something";
}

But if you do not want a "variable" to change, use a constant.

knittl
  • 246,190
  • 53
  • 318
  • 364