Search code examples
phpclass-constants

define constant in php


how do I define a constant inside a function

eg.

class {

     public test;

     function tester{

      const test = "abc";

     }

  }

Solution

  • You are doing fine but you need to put the const at the class level not inside the function eg:

    class {
     const TEST = "abc"; 
     public $test2;
    
     function tester{
      // code here
     }
    }
    

    More info here.

    Also, you were missing $ in public variable test