You are here: Re: Defining a callback Func for preg_replace_callback(), within some class's method « PHP Programming Language « IT news, forums, messages
Re: Defining a callback Func for preg_replace_callback(), within some class's method

Posted by jody.florian on 09/27/32 11:37

example.php:
<?php
class myclass {
protected $vars = array();
function addVar($name,$val){ $this->vars[$name] = $val; }
function output(){
$fileContents = file_get_contents("my.template");
$outputString = preg_replace_callback(
'@{([A-Z]+)}@',
create_function('$matcharray','return
$this->vars[$matches[1]];'),
$fileContents);
echo $outputString;
} }

$obj = new myclass();
$obj->addVar("NAME", "bob");
$obj->addVar("MOBILE", "0207 PHP RULES");
$obj->addVar("EMAIL", "b...@bob.com");
$obj->output();
?>

my.template :
<ul>
<li>{NAME}</li>
<li>{MOBILE}</li>
<li>{EMAIL}</li>
</ul>

It produces the error:

Fatal error: Using $this when not in object context in
/www/thisproblem/example.php(10) : runtime-created function on line 1

In the example at
http://uk.php.net/manual/en/function.preg-replace-callback.php

They're not implementing a callback function with create_function() in
a class context, as opposed to my example above.

Since it's a) not wise and b) not possible to declare a function within
a method (since it would be re-declared each time the method's used)
I'm still quite at a loss myself

I'm hazy on my lambda calculus but surely there's a way round this,
other than creating a method solely as a one-off callback?

Cheers

--
J Florian

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация