MediaWiki REL1_32
DeprecationHelper.php
Go to the documentation of this file.
1<?php
46
55 protected $deprecatedPublicProperties = [];
56
68 protected function deprecatePublicProperty(
69 $property, $version, $class = null, $component = null
70 ) {
71 $this->deprecatedPublicProperties[$property] = [ $version, $class ?: get_class(), $component ];
72 }
73
74 public function __get( $name ) {
75 if ( isset( $this->deprecatedPublicProperties[$name] ) ) {
76 list( $version, $class, $component ) = $this->deprecatedPublicProperties[$name];
77 $qualifiedName = $class . '::$' . $name;
78 wfDeprecated( $qualifiedName, $version, $component, 3 );
79 return $this->$name;
80 }
81
82 $qualifiedName = get_class() . '::$' . $name;
83 if ( $this->deprecationHelperGetPropertyOwner( $name ) ) {
84 // Someone tried to access a normal non-public property. Try to behave like PHP would.
85 trigger_error( "Cannot access non-public property $qualifiedName", E_USER_ERROR );
86 } else {
87 // Non-existing property. Try to behave like PHP would.
88 trigger_error( "Undefined property: $qualifiedName", E_USER_NOTICE );
89 }
90 return null;
91 }
92
93 public function __set( $name, $value ) {
94 if ( isset( $this->deprecatedPublicProperties[$name] ) ) {
95 list( $version, $class, $component ) = $this->deprecatedPublicProperties[$name];
96 $qualifiedName = $class . '::$' . $name;
97 wfDeprecated( $qualifiedName, $version, $component, 3 );
98 $this->$name = $value;
99 return;
100 }
101
102 $qualifiedName = get_class() . '::$' . $name;
103 if ( $this->deprecationHelperGetPropertyOwner( $name ) ) {
104 // Someone tried to access a normal non-public property. Try to behave like PHP would.
105 trigger_error( "Cannot access non-public property $qualifiedName", E_USER_ERROR );
106 } else {
107 // Non-existing property. Try to behave like PHP would.
108 $this->$name = $value;
109 }
110 }
111
119 // Easy branch: check for protected property / private property of the current class.
120 if ( property_exists( $this, $property ) ) {
121 // The class name is not necessarily correct here but getting the correct class
122 // name would be expensive, this will work most of the time and getting it
123 // wrong is not a big deal.
124 return __CLASS__;
125 }
126 // property_exists() returns false when the property does exist but is private (and not
127 // defined by the current class, for some value of "current" that differs slightly
128 // between engines).
129 // Since PHP triggers an error on public access of non-public properties but happily
130 // allows public access to undefined properties, we need to detect this case as well.
131 // Reflection is slow so use array cast hack to check for that:
132 $obfuscatedProps = array_keys( (array)$this );
133 $obfuscatedPropTail = "\0$property";
134 foreach ( $obfuscatedProps as $obfuscatedProp ) {
135 // private props are in the form \0<classname>\0<propname>
136 if ( strpos( $obfuscatedProp, $obfuscatedPropTail, 1 ) !== false ) {
137 $classname = substr( $obfuscatedProp, 1, -strlen( $obfuscatedPropTail ) );
138 if ( $classname === '*' ) {
139 // sanity; this shouldn't be possible as protected properties were handled earlier
140 $classname = __CLASS__;
141 }
142 return $classname;
143 }
144 }
145 return false;
146 }
147
148}
deprecatePublicProperty( $property, $version, $class=null, $component=null)
Mark a property as deprecated.
__set( $name, $value)
__get( $name)
trait DeprecationHelper
Use this trait in classes which have properties for which public access is deprecated.
deprecationHelperGetPropertyOwner( $property)
Like property_exists but also check for non-visible private properties and returns which class in the...
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition deferred.txt:11
Allows to change the fields on the form that will be generated $name
Definition hooks.txt:302
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
$property