How to resolve the algorithm Break OO privacy step by step in the PHP programming language
How to resolve the algorithm Break OO privacy step by step in the PHP programming language
Table of Contents
Problem Statement
Show how to access private or protected members of a class in an object-oriented language from outside an instance of the class, without calling non-private or non-protected members of the class as a proxy.
The intent is to show how a debugger, serializer, or other meta-programming tool might access information that is barred by normal access methods to the object but can nevertheless be accessed from within the language by some provided escape hatch or reflection mechanism.
The intent is specifically not to demonstrate heroic measures such as peeking and poking raw memory.
Note that cheating on your type system is almost universally regarded
as unidiomatic at best, and poor programming practice at worst.
Nonetheless, if your language intentionally maintains a double-standard for OO privacy, here's where you can show it off.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Break OO privacy step by step in the PHP programming language
Sourcecode 1
- The first snippet of code creates a class called
SimpleClass
with a private propertyanswer
set to the string"hello\"world\nforever :)"
. - It then creates an instance of the
SimpleClass
class and usesob_start()
to start buffering output. - It then uses
var_export()
to export the class to a string, but removes the__set_state()
method from the result usingpreg_replace()
. - It then uses
eval()
to evaluate the string and create a new instance of theSimpleClass
class with the same private property value as the original class. - Finally, it prints the value of the
answer
property of the new class instance.
Sourcecode 2
- The second snippet of code creates a class called
SimpleClass
with a private propertyanswer
set to the integer42
. - It then creates an instance of the
SimpleClass
class and uses(array)
to convert it to an array. - It then prints the value of the
answer
property of the array using the array syntax["\0SimpleClass\0answer"]
.
Sourcecode 3
- The third snippet of code creates a class called
fragile
with a private propertyfoo
set to the string'bar'
. - It then creates an instance of the
fragile
class and usesReflectionObject()
to create a reflection object for the class. - It then uses
getProperty()
to get the reflection property for thefoo
property and usessetAccessible(true)
to make the property accessible. - It then uses
getValue()
to get the value of thefoo
property and prints it. - It then uses
setValue()
to set the value of thefoo
property to the string'haxxorz!'
and prints the new value. - Finally, it prints the
fragile
class instance to show that the value of thefoo
property has been changed.
Source code in the php programming language
<?php
class SimpleClass {
private $answer = "hello\"world\nforever :)";
}
$class = new SimpleClass;
ob_start();
// var_export() expects class to contain __set_state() method which would import
// data from array. But let's ignore this and remove from result the method which
// sets state and just leave data which can be used everywhere...
var_export($class);
$class_content = ob_get_clean();
$class_content = preg_replace('"^SimpleClass::__set_state\("', 'return ', $class_content);
$class_content = preg_replace('"\)$"', ';', $class_content);
$new_class = eval($class_content);
echo $new_class['answer'];
<?php
class SimpleClass {
private $answer = 42;
}
$class = new SimpleClass;
$classvars = (array)$class;
echo $classvars["\0SimpleClass\0answer"];
<?php
class fragile {
private $foo = 'bar';
}
$fragile = new fragile;
$ro = new ReflectionObject($fragile);
$rp = $ro->getProperty('foo');
$rp->setAccessible(true);
var_dump($rp->getValue($fragile));
$rp->setValue($fragile, 'haxxorz!');
var_dump($rp->getValue($fragile));
var_dump($fragile);
You may also check:How to resolve the algorithm Documentation step by step in the Objeck programming language
You may also check:How to resolve the algorithm CSV data manipulation step by step in the OCaml programming language
You may also check:How to resolve the algorithm Increment a numerical string step by step in the 8080 Assembly programming language
You may also check:How to resolve the algorithm Literals/Integer step by step in the Groovy programming language
You may also check:How to resolve the algorithm Real constants and functions step by step in the Modula-3 programming language