MediaWiki 1.42.0
GhostFieldAccessTrait.php
Go to the documentation of this file.
1<?php
22
36trait GhostFieldAccessTrait {
37
46 private function getGhostFieldValue( string $name, string ...$aliases ) {
47 if ( isset( $this->$name ) ) {
48 return $this->$name;
49 }
50
51 $data = (array)$this;
52
53 // Protected variables have a '*' prepended to the variable name.
54 // These prepended values have null bytes on either side.
55 $protectedName = "\x00*\x00{$name}";
56 if ( isset( $data[$protectedName] ) ) {
57 return $data[$protectedName];
58 }
59
60 // Private variables have the class name prepended to the variable name.
61 // These prepended values have null bytes on either side.
62 $thisClass = get_class( $this );
63 $privateName = "\x00{$thisClass}\x00{$name}";
64 if ( isset( $data[$privateName] ) ) {
65 return $data[$privateName];
66 }
67
68 // Check old aliased class names as well.
69 foreach ( $aliases as $thisClass ) {
70 $privateName = "\x00{$thisClass}\x00{$name}";
71 if ( isset( $data[$privateName] ) ) {
72 return $data[$privateName];
73 }
74 }
75
76 return null;
77 }
78
88 private function restoreAliasedGhostField( string $name, string ...$aliases ): void {
89 $data = (array)$this;
90 foreach ( $aliases as $thisClass ) {
91 $privateName = "\x00{$thisClass}\x00{$name}";
92 if ( isset( $data[$privateName] ) ) {
93 $this->$name = $data[$privateName];
94 unset( $data[$privateName] );
95 return;
96 }
97 }
98 }
99}