PHP - constant definition inside a class

How to define a constant inside a class and make it visible only when called in the context of the class?

.... something like Foo::app()->MYCONSTANT;

(and if called as MYCONSTANT to ignore)

+47
php class constants
May 05 '11 at 3:12
source share
5 answers

See Class Constants :

 class MyClass { const MYCONSTANT = 'constant value'; function showConstant() { echo self::MYCONSTANT. "\n"; } } echo MyClass::MYCONSTANT. "\n"; $classname = "MyClass"; echo $classname::MYCONSTANT. "\n"; // As of PHP 5.3.0 $class = new MyClass(); $class->showConstant(); echo $class::MYCONSTANT."\n"; // As of PHP 5.3.0 

In this case, echoing MYCONSTANT itself will increase the notification of the undefined constant and display the constant name converted to the string: "MYCONSTANT" .




EDIT . Perhaps this is what you are looking for static properties / variables :

 class MyClass { private static $staticVariable = null; public static function showStaticVariable($value = null) { if ((is_null(self::$staticVariable) === true) && (isset($value) === true)) { self::$staticVariable = $value; } return self::$staticVariable; } } MyClass::showStaticVariable(); // null MyClass::showStaticVariable('constant value'); // "constant value" MyClass::showStaticVariable('other constant value?'); // "constant value" MyClass::showStaticVariable(); // "constant value" 
+79
May 05 '11 at 3:14 a.m.
source share
 class Foo { const BAR = 'baz'; } echo Foo::BAR; 

This is the only way to make class constants. These constants are always available worldwide through Foo::BAR , but they are not available only with BAR .

To achieve syntax like Foo::baz()->BAR , you need to return an object from the baz() function of the Foo class, which has the BAR property. This is not a constant, though. Any constant that you define is always accessible globally from anywhere and cannot be limited to function call functions.

+7
May 05 '11 at 3:17
source share

This is an old question, but now in PHP 7.1 you can define constant visibility.

Example

 <?php class Foo { // As of PHP 7.1.0 public const BAR = 'bar'; private const BAZ = 'baz'; } echo Foo::BAR.PHP_EOL; echo Foo::BAZ.PHP_EOL; ?> 

The output of the above example in PHP 7.1:

 bar

 Fatal error: Uncaught Error: Cannot access private const Foo :: BAZ in ...

Note: Starting with PHP 7.1.0, visibility modifiers are allowed for class constants.

More here

+7
Aug 12 '16 at 7:28
source share

This is a pretty old question, but maybe this answer might help someone else.

You can emulate a public constant that is limited within the class by applying the last keyword to a method that returns the given value, for example:

 class Foo { // This is a private constant final public MYCONSTANT() { return 'MYCONSTANT_VALUE'; } } 

The final keyword in the method prevents redefining the extension method. You can also put the final keyword before the class declaration, in which case the keyword prevents the class from inheriting.

To get almost what Alex was looking for, you can use the following code:

 final class Constants { public MYCONSTANT() { return 'MYCONSTANT_VALUE'; } } class Foo { static public app() { return new Constants(); } } 

The emulated constant value will be available as follows:

 Foo::app()->MYCONSTANT(); 
+6
Mar 29 '13 at 17:51
source share

You can define a class constant in php. But your class constant would be accessible from any instance of the object. This is php functionality. However, with php7.1 , you can define your class constants using access modifiers ( public , private or protected ).

A workaround would be to define your constant as private or protected , and then make them readable through a static function . This function should return constant values ​​only when called from a static context.

You can also create this static function in the parent class and simply inherit this parent class on all other classes to make it the default functionality.

Credits: http://dwellupper.io/post/48/defining-class-constants-in-php

0
Nov 13 '17 at 16:40
source share



All Articles