Php - Static Initialization (Variable / Constant)

Card Puncher Data Processing

About

static in the context of php

Syntax Static Variable

  • In static member, you can use the global variable
private static $pageRedirectionsFilePath = __DIR__ . "/404managerRedirect.conf";

Dynamic Static Variable

You can't use function in constant initialization. Below are methods that you can use to get the same behavior

Function

replace a const

const MY_CONST=myFunction()

with a static function

private static function getMyConst()
{
  return myFunction();
}

Init function

Php - Constant

  • Function init
class Foo {
  private static $bar;
  static function init()
  {
    self::$bar = array(…);
  }
}
Foo::init();

Documentation / Reference





Discover More
Card Puncher Data Processing
Php - Constant

in PHP See Class ConstantsClass Constants differ from normal static variables in that you don't use the $ symbol to declare or use them. Abstract static method With reflection and the...



Share this page:
Follow us:
Task Runner