MediaWiki REL1_37
StubObject Class Reference

Class to implement stub globals, which are globals that delay loading the their associated module code by deferring initialisation until the first method call. More...

Inheritance diagram for StubObject:

Public Member Functions

 __call ( $name, $args)
 Function called by PHP if no function with that name exists in this object.
 
 __construct ( $global=null, $class=null, $params=[])
 
 __get ( $name)
 Function called by PHP if no property with that name exists in this object.
 
 __set ( $name, $value)
 Function called by PHP if no property with that name exists in this object.
 
 _call ( $name, $args)
 Function called if any function exists with that name in this object.
 
 _get ( $name)
 Wrapper for __get(), similar to _call() above.
 
 _newObject ()
 Create a new object to replace this stub object.
 
 _set ( $name, $value)
 Wrapper for __set(), similar to _call() above.
 
 _unstub ( $name='_unstub', $level=2)
 This function creates a new object of the real class and replace it in the global variable.
 

Static Public Member Functions

static isRealObject ( $obj)
 Returns a bool value whenever $obj is a stub object.
 
static unstub (&$obj)
 Unstubs an object, if it is a stub object.
 

Protected Attributes

null string $class
 
null callable $factory
 
null string $global
 
array $params
 

Detailed Description

Class to implement stub globals, which are globals that delay loading the their associated module code by deferring initialisation until the first method call.

Note on reference parameters:

If the called method takes any parameters by reference, the __call magic here won't work correctly. The solution is to unstub the object before calling the method.

Note on unstub loops:

Unstub loops (infinite recursion) sometimes occur when a constructor calls another function, and the other function calls some method of the stub. The best way to avoid this is to make constructors as lightweight as possible, deferring any initialisation which depends on other modules. As a last resort, you can use StubObject::isRealObject() to break the loop, but as a general rule, the stub object mechanism should be transparent, and code which refers to it should be kept to a minimum.

@newable

Definition at line 51 of file StubObject.php.

Constructor & Destructor Documentation

◆ __construct()

StubObject::__construct (   $global = null,
  $class = null,
  $params = [] 
)
Stability: stable
to call
Parameters
string | null$globalName of the global variable.
string | callable | null$className of the class of the real object or a factory function to call
array$paramsParameters to pass to constructor of the real object.

Reimplemented in DeprecatedGlobal.

Definition at line 72 of file StubObject.php.

References $class, $global, and $params.

Member Function Documentation

◆ __call()

StubObject::__call (   $name,
  $args 
)

Function called by PHP if no function with that name exists in this object.

Parameters
string$nameName of the function called
array$argsArguments
Returns
mixed

Definition at line 148 of file StubObject.php.

References $args, and _call().

◆ __get()

StubObject::__get (   $name)

Function called by PHP if no property with that name exists in this object.

Parameters
string$nameName of the property to get
Returns
mixed

Definition at line 170 of file StubObject.php.

References _get().

◆ __set()

StubObject::__set (   $name,
  $value 
)

Function called by PHP if no property with that name exists in this object.

Parameters
string$nameName of the property to set
mixed$valueNew property value

Definition at line 192 of file StubObject.php.

References _set().

◆ _call()

StubObject::_call (   $name,
  $args 
)

Function called if any function exists with that name in this object.

It is used to unstub the object. Only used internally, PHP will call self::__call() function and that function will call this function. This function will also call the function with the same name in the real object.

Parameters
string$nameName of the function called
array$argsArguments
Returns
mixed

Definition at line 118 of file StubObject.php.

References $args, and _unstub().

Referenced by __call().

◆ _get()

StubObject::_get (   $name)

Wrapper for __get(), similar to _call() above.

Parameters
string$nameName of the property to get
Returns
mixed

Definition at line 158 of file StubObject.php.

References $global, and _unstub().

Referenced by __get().

◆ _newObject()

StubObject::_newObject ( )

Create a new object to replace this stub object.

Returns
object

Reimplemented in DeprecatedGlobal, StubGlobalUser, and StubUserLang.

Definition at line 127 of file StubObject.php.

References $class, $factory, and $params.

Referenced by _unstub().

◆ _set()

StubObject::_set (   $name,
  $value 
)

Wrapper for __set(), similar to _call() above.

Parameters
string$nameName of the property to set
mixed$valueNew property value

Definition at line 180 of file StubObject.php.

References $global, and _unstub().

Referenced by __set().

◆ _unstub()

StubObject::_unstub (   $name = '_unstub',
  $level = 2 
)

This function creates a new object of the real class and replace it in the global variable.

This is public, for the convenience of external callers wishing to access properties, e.g. eval.php

Parameters
string$nameName of the method called in this object.
int$levelLevel to go in the stack trace to get the function who called this function.
Returns
object The unstubbed version of itself
Exceptions
MWException

Reimplemented in StubGlobalUser.

Definition at line 208 of file StubObject.php.

References $global, _newObject(), wfDebug(), and wfGetCaller().

Referenced by _call(), _get(), and _set().

◆ isRealObject()

static StubObject::isRealObject (   $obj)
static

Returns a bool value whenever $obj is a stub object.

Can be used to break a infinite loop when unstubbing an object.

Parameters
object$objObject to check.
Returns
bool True if $obj is not an instance of StubObject class.

Definition at line 89 of file StubObject.php.

◆ unstub()

static StubObject::unstub ( $obj)
static

Unstubs an object, if it is a stub object.

Can be used to break a infinite loop when unstubbing an object or to avoid reference parameter breakage.

Parameters
object&$objObject to check.
Returns
void

Definition at line 101 of file StubObject.php.

Referenced by ParserOptions\__construct(), LogPage\actionText(), and ContentHandler\getPageLanguage().

Member Data Documentation

◆ $class

null string StubObject::$class
protected

Definition at line 56 of file StubObject.php.

Referenced by __construct(), and _newObject().

◆ $factory

null callable StubObject::$factory
protected

Definition at line 59 of file StubObject.php.

Referenced by _newObject().

◆ $global

null string StubObject::$global
protected

Definition at line 53 of file StubObject.php.

Referenced by __construct(), _get(), _set(), and _unstub().

◆ $params

array StubObject::$params
protected

Definition at line 62 of file StubObject.php.

Referenced by __construct(), and _newObject().


The documentation for this class was generated from the following file: