Ray can now display the values of private properties original
Our handy Ray debugging app gained a cool new trick: it can now display the values of private properties and results of private methods.
Posts tagged with reflection
Our handy Ray debugging app gained a cool new trick: it can now display the values of private properties and results of private methods.
Last week, Caleb tweeted about a nifty function called invade - that he had made to easily work with private properties and methods.
😈 Whatcha think of my new favorite helper method?
— Caleb Porzio (@calebporzio) February 11, 2022
That property/method protected or private? No problemoooo 🔓 pic.twitter.com/HqMXKKpRsJ
He added that invade function to Livewire. Because I could see myself using this in non-Laravel projects, I packaged up the function in a new package called spatie/invade.
Join 9,500+ smart developers
Get my monthly newsletter with what I learn from running Spatie, building Oh Dear, and maintaining 300+ open source packages. Practical takes on Laravel, PHP, and AI that you can actually use.
"Freek publishes a super resourceful and practical newsletter. A must for anyone in the Laravel space"
No spam. Unsubscribe anytime. You can also follow me on X.
PHP has a few ways to access private properties this: reflection, closures and array casting.
Read more [www.lambda-out-loud.com]
The awesome Roave team has recently released v2 of their BetterReflection package. It can do anything PHP's native reflection API can, but without actually autoloading the code.
The operational concept is quite simple, really:
- We scan your codebase for files matching the one containing your class. This is fully configurable, but by default we use some ugly autoloader hacks to find the file without wasting disk I/O.
- We feed your PHP file to PHP-Parser
- We analyse the produced AST and wrap it in a matching Roave\BetterReflection\Reflection* class instance, ready for you to consume it.
Read all about it on Marco Pivetta's blog: https://ocramius.github.io/blog/roave-better-reflection-v2.0/
Here's a great story by Collin O'Dell, maintainer of league/commonmark amongst other things, on how he was able to extract the class definitions out of obfuscated PHP source files.
Per the framework's license, decrypting the IonCube-protected code was not allowed. This meant it was impossible to recover the original source code. However, I could require those files and execute them in PHP, which would cause those classes to become usable in code. So how does one figure out what code just got loaded & executed?
https://www.colinodell.com/blog/201708/generating-ide-stubs-ioncube-encoded-classes
At the PHP Benelux conference James Titcumb did a presentation on the BetterReflection library he's working on. You can view the slides of his presentation on SlideShare.
The BetterReflection library has a few advantages over PHP's native reflection capabilities (copied from the docs):
Consider this class:
class MonkeyPatchMe
{
function getMessage() : string
{
return 'hello everybody';
}
}
The body of the getMessage-function can be replaced using this code:
$classInfo = ReflectionClass::createFromName('MonkeyPatchMe');
$methodInfo = $classInfo->getMethod('getMessage');
$methodInfo->setBody(function() {
return 'bye everybody';
});
$monkeyPatchedClass = new MonkeyPatchMe();
$monkeyPatchedClass->getMessage() // returns 'bye everybody'
Behind the scenes this voodoo works by copying the original class to another file, doing a replacement of the function body there and loading up that class.
I'll be keeping my eye on how the BetterReflection library evolves. You can read all about it's current functionality in the docs on GitHub.