Skip to content

Watching Inaccessible Properties For Changes

ifsale edited this page Dec 4, 2013 · 1 revision

Here is a good trick to find when a protected or private property changes in your application. We will be using the method PtcDebug::watch( ), but since the default behavior is to look in the global scope, we will add a closure as callback to find our property. For this task it's probably better to use the ptc-helpers functions , as we will be typing less.

Here is the peace of code that will return our protected / private property:


ptc_watch( 'some_var' , function( )
{ 
	global $application; 
	$app = ptc_get_prop( $application , 'api' ); // PtcHandyMan::getProperty( );
	return ( $app ) ? $app : null;
} );

Now, let's try to analyze what is actually happening here:

  • We use a closure as callback to retrieve our protected property.
  • We make $application visible inside the closure, that is our object.
  • We use ptc_get_prop( ) to get the protected property api from $application object.
  • We make sure that if the property is not found we return null, this will not trigger false positives.

The tickHandler of the PtcDebug component, will take care of calling the closure for you and checking if the property has actually changed value.