Define constant in php

how to define a constant inside a function

eg.

class {

     public test;

     function tester{

      const test = "abc";

     }

  }
+3
source share
4 answers

Everything is fine with you, but you need to put it constat the class level not inside the function, for example:

class {
 const TEST = "abc"; 
 public $test2;

 function tester{
  // code here
 }
}

More here

Also, you were not $in the public variabletest

+6
source

I think you want a class constant

class SomeClass {

  const test = "abc";

  function tester() {
    return; 
  }

}
+5
source

. const self:: .

class TestClass
{
    const test = "abc";

    function tester()
    {
        return self::test;
    }
}

$testClass = new TestClass();
//abcabc
echo $testClass->tester();
echo TestClass::test;

, , ::

+4

Source: https://habr.com/ru/post/1743764/


All Articles