[Phpwm] Accessing class variable from sub function

Richard Harrison php at rjharrison.org
Wed Apr 16 12:31:36 BST 2008


Hi Mike.

Take a look at the "callback type": 
http://uk.php.net/manual/en/language.pseudo-types.php#language.types.callback

You can pass an object+method as an array as the callback... so if you 
have a method of your $this object as the callback:-

<?php

class myclass {

    public $someData = array(123);

    function replaceText($someText)
    {
        preg_replace_callback('/eg/' , array($this, '_callback'), 
$someText);
    } 


    protected function _callback($matches) {
        var_dump($this->someData);
    }   

}

$a = new myclass();
$a->replaceText('eg');


Best regards,

Richard.

Mike Tipping wrote:
> This is probably a silly question but I can't work it out.
>
> I'm using preg_replace_callback which takes a callback function as a
> parameter. The call to preg_replace_callback is within a class and I need
> the callback function to have access to the classes properties, but can't
> work out how to give it access.
>
> So with something like this:
>
> Class myclass {
>
>   public someData = array();
>   
>   function replaceText($someText) {
>     function callbackFunc($matches) {
>       print_r(xxx);
>     }    
>     reg_replace_callback('/{id="1"}/' ," callbackFunc", $fileContent);
>   }  
>
> }
>
> What do I need to replace xxx with to access the array someData?
>
> Or am I approaching this in completely the wrong way?
>
> Cheers
>
> Mike
>
>
>
> _______________________________________________
> Phpwm mailing list
> Phpwm at mailman.lug.org.uk
> https://mailman.lug.org.uk/mailman/listinfo/phpwm
>
>   




More information about the Phpwm mailing list