26

It might be nice to be able to define a local constant within the scope of a function in PHP

This way anyone reading the function would know that that definition is final within the function and doesn't affect any external code

Something like this (Not valid syntax):

public function laughManiacally($count){
  const LAUGH = 'HA';

  for($i=0;$i<$count;$i++){
    echo LAUGH;
  }
};  

Or possibly (Again not valid):

...
final $laugh = 'HA';
...

Is there anyway to do this? And if not why not?

UPDATE

The way that JavaScript allows block level constant declaration is similar to the functionality I was searching for

function laughManiacally(count) {
  const LAUGH = 'Ha';

  for (let i = 0; i < count; i++) {
    console.log(LAUGH);
  }
}
Arth
  • 12,060
  • 5
  • 34
  • 67

3 Answers3

15

You should have

echo name_of_class::LAUGH

Otherwise PHP will be looking for a global constant that was created with define().


followup:

You can also only define constants at the object level, e.g.

class foo {
   const BAR = 'baz';  // valid

   function foo() {
      const BAR = 'baz'; // invalid. can't define constants INSIDE a method.
   }
}
Marc B
  • 348,685
  • 41
  • 398
  • 480
  • still giving error, may be i am doing in a wrong way:-https://eval.in/654344 . correct me – Anant Kumar Singh Oct 03 '16 at 15:31
  • Cool, that's what I experienced, but is there anyway to mark a local variable as read-only? – Arth Oct 03 '16 at 15:38
  • it's a local variable. it can't be accessed from "outside" the function, so what's the point of making it read only? If you want it read only, then don't write to it (except for initialization). – Marc B Oct 03 '16 at 15:41
  • 1
    For the same reason I wouldn't use a public class variable as a class constant.. I want to stop myself and other team members from writing to it. I was also curios.. I guess commenting a local variable is probably as close as I'm going to get though! – Arth Oct 03 '16 at 15:49
11

No, there are no function-level constants in PHP. The closest thing is a class constant:

class A
{
    const LAUGH = 'HA';

    function laughManiacally($count)
    {
        for ($i=0; $i < $count; ++$i) {
            echo static::LAUGH;
        }
    }
}

$obj = new A();
$obj->laughManiacally(5);

Output:

HAHAHAHAHA

I have also tried final $laugh = 'HA';

The final keyword cannot be applied to class properties - only to methods.

ShiraNai7
  • 6,129
  • 2
  • 23
  • 26
  • 4
    I normally find the documentation doesn't specify things that aren't there.. and as a result it isn't easy to find rarely used constructs. I was primarily using those syntax attempts to demonstrate the kind of functionality I was hoping for. – Arth Oct 03 '16 at 15:45
-1

I think you're confusing const with scope here.

function foo() {
    $x = 1;
}

I don't need to declare $x as a const because it's already scoped to just foo(). I would have to explicitly raise it to the global scope to use it elsewhere (which is a bad idea for many reasons). $x can be changed, but only if you do so within your function, or edit the code.

Ironically, constants set by using define are globally scoped, with const generally being global, provided you're using an autoloader

miken32
  • 39,644
  • 15
  • 91
  • 133
Machavity
  • 29,816
  • 26
  • 86
  • 96