eph baum dot dev

← Back to blog

How PHP 5.4's Closure - -bind Saved me From Reflection

Published on 06/15/2015 06:00 PM by Eph Baum

A few weeks ago I was faced with a problem: I needed to access private variables within a Library class in our Laravel 4 based web application. The library we’re using, installed with Composer, did not offer a way to access any of its properties, instead it offered two public methods that simply returned concatenated private variable strings. I needed only one of the two elements.

As an example, it looked a bit like this:

class Library  
{
  private $privateProperty1 = "value1";
  private $privateProperty2 = "value2";
  public function returnConcatProps()
  {
   return 'The private properties are: '.
           $this->privateProperty1 . ' ' .
           $this->privateProperty2;
  }
}

$lib = new Library();
echo $lib->returnConcatProps() . "\n";

echo $lib->privateProperty1;  

In this case, if you try to access those private properties of the Library class:

Private means private!

Extending the class isn’t possible as it would still leave the properties I needed unavailable, and modifying directly wouldn’t work as an update in the future would overwrite the changes. The only way to get at these properties historically is to use Reflection.

This, to me, didn’t seem right for a few reasons. It feels, and I’m using a technical term here, ooky. Also, if you check out the documentation for the Reflection class in PHP, it says:

Introduction

PHP 5 comes with a complete reflection API that adds the ability to reverse-engineer classes, interfaces, functions, methods and extensions. Additionally, the reflection API offers ways to retrieve doc comments for functions, classes and methods.

Please note that certain parts of the internal API are missing the necessary code to work with the Reflection extension. E.g., an internal PHP class might be missing reflection data for properties. These few cases are considered bugs, however, so they should be discovered and fixed.

Using Reflection in this case, while something that would have likely accomplished my goal, seemed rather kludgy.

I won’t lie, I’ve still not quite been able to fully grok the purpose behind the Reflection class, exactly. It allows you to modify / access code during runtime. That is not what I want in this case.

Meanwhile, in PHP 5.4, Closure::bind has been introduced. It is light weight and does exactly what I need using a simple closure.

Here’s an example of how it looks:

<?php  
  class Library
  {
    private $privateProperty1 = "value1";
    private $privateProperty2 = "value2";
    public function returnConcatProps()
    {
     return 'The private properties are: '.
             $this->privateProperty1 . ' ' .
             $this->privateProperty2;
    }
  }

  $lib = new Library();
  echo $lib->returnConcatProps() . "\n";

  class MyClass
  {
    public function getPrivatePropertyOneFromLibrary()
    {
      $lib = new Library();
      $closure = function( Library $library ) {
         return $library->privateProperty1;
      };
      $bind = Closure::bind( $closure, null, $lib );
      return $bind( $lib );
    }

    public function getPrivatePropertyTwoFromLibrary()
    {
      $lib = new Library();

      $closure = function( Library $library ) {
         return $library->privateProperty2;
      };

      $bind = Closure::bind( $closure, null, $lib );
      return $bind( $lib );
    }
  }

  $class = new MyClass();

  echo $class->getPrivatePropertyOneFromLibrary() . "\n";
  echo $class->getPrivatePropertyTwoFromLibrary() . "\n";
?>

Showing the private goodies!

That’s that. With Closure::bind() I can essentially ‘extend’ any class, accessing their private properties, using PHP’s native Closure class and anonymous functions.

Written by Eph Baum

  • AI

    AI

    This may read as alarmist. It might not be alarmist enough. On AI as sophisticated autocomplete, not thinking—and why that gap matters to the future.

  • Slow Down

    Slow Down

    Speed worship is anxiety wearing a productivity costume. The case for slowing down—and why going fast is often how you screw everything up.

  • Making Brutalist Design Accessible: A Journey in WCAG AA Compliance

    Making Brutalist Design Accessible: A Journey in WCAG AA Compliance

    How I transformed my brutalist blog theme to meet WCAG AA accessibility standards while preserving its vibrant, random aesthetic. Talking about contrast ratios, color theory, and inclusive design.

  • Building Horror Movie Season: A Journey in AI-Augmented Development

    Building Horror Movie Season: A Journey in AI-Augmented Development

    How I built a production web app primarily through 'vibe coding' with Claude, and what it taught me about the future of software development. A deep dive into AI-augmented development, the Horror Movie Season app, and reflections on the evolving role of engineers in the age of LLMs.

  • Chaos Engineering: Building Resiliency in Ourselves and Our Systems

    Chaos Engineering: Building Resiliency in Ourselves and Our Systems

    Chaos Engineering isn't just about breaking systems — it's about building resilient teams, processes, and cultures. Learn how deliberate practice strengthens both technical and human architecture, and discover "Eph's Law": If a single engineer can bring down production, the failure isn't theirs — it's the process.

  • Using LLMs to Audit and Clean Up Your Codebase: A Real-World Example

    Using LLMs to Audit and Clean Up Your Codebase: A Real-World Example

    How I used an LLM to systematically audit and remove 228 unused image files from my legacy dev blog repository, saving hours of manual work and demonstrating the practical value of AI-assisted development.