My particular scenario involves doing some text transformation using regular expressions within a private method. The private method calls preg_replace_callback, but is seem
Can you create a separate helper class for text processing, and create the callback method within that class (it can be public there)? You can pass in a reference to the original class(object) if you need access to specific instance data.
The public interface to your main class remains consistent and clean, and you pull specific string processing out of a class it probably doesn't belong in anyway...
Yes, it seems you can:
<?php
//this works
class a {
private function replaceCallback($m) { return 'replaced'; }
public function test() {
$str = " test test ";
$result = preg_replace_callback('/test/', array($this, 'replaceCallback'), $str);
echo $result;
}
}
$a = new a();
$a->test();
//this doesn't work
$result = preg_replace_callback('/test/', array(new a(), 'replaceCallback'), ' test test ');
echo $result;
So it seems that preg_replace_callback()
, or PHP's callback mechanism, is aware of the scope in which it was called.
Tested on 5.2.8
Can you not use protected? I'd need to know what the relationship of these two classes were in relation to each other to be more clear, but you should be able to extend the class and use the new protected methods you have inherited without making them public.
Hope this helps!
Cheers