473,387 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

return function in class

I had this little bit of code:

function parseTemplateData($file){
$file2=preg_replace_callback(
"/<edit(.*?)>/s",
"parseEditTag",
$file);
echo $file2;
}

and that did what I needed, so I thought I'll put it in a class:

class FOO{

public function parseTemplateData($file){
$file2=preg_replace_callback(
"/<edit(.*?)>/s",
"parseEditTag",
$file);
echo $file2;
}

public function parseEditTag($tag_content){
....

}

It is, of course looking for parseEditTag *outside* the class. So, I
thought:

$file2=preg_replace_callback(
"/<edit(.*?)>/s",
"$this->parseEditTag",
$file);

Which failed.

Then I thought:

$file2=preg_replace_callback(
"/<edit(.*?)>/s",
EDIT_PAGE::parseEditTag(),
$file);

Now that calls the function, but doesn't pass in the match.

So I thought I'd try to use create_function. But even if I could
figure out how to pass in the matches, I still can't get it to call the
method in the class.

So, I'm thinking there must be another plan. Or another beer.

Maybe I'm just thinking too perl!

Jeff
Jun 27 '08 #1
2 1292
On Wed, 18 Jun 2008 23:07:40 +0200, Jeff <jeff@spam_me_not.comwrote:
I had this little bit of code:

function parseTemplateData($file){
$file2=preg_replace_callback(
"/<edit(.*?)>/s",
"parseEditTag",
$file);
echo $file2;
}

and that did what I needed, so I thought I'll put it in a class:

class FOO{

public function parseTemplateData($file){
$file2=preg_replace_callback(
"/<edit(.*?)>/s",
"parseEditTag",
$file);
echo $file2;
}

public function parseEditTag($tag_content){
...

}
Valid callbacks are:
$callback = 'function_name';
$callback = array($instanceofObject,'method_name');
$callback = array('ClassName','static_method_name'));

If you went to the manual for preg_replace_callback:
http://nl2.php.net/preg_replace_callback
...and clicked on 'callback', you end up here:
http://nl2.php.net/manual/en/languag...ypes..callback

<?php
class Foo{
function test(){
echo preg_replace_callback('/a/',array($this,'funcname'),"acac");
echo preg_replace_callback('/a/',array('Bar','funcname'),"acac");
echo preg_replace_callback('/a/','funcname',"acacacaca");
}
function funcname(){
return 'b';
}
}
class Bar{
static function funcname(){
return 'd';
}
}
function funcname(){
return 'e';
}
$f = new Foo();
$f->test();
?>
--
Rik Wasmus
....spamrun finished
Jun 27 '08 #2
Rik Wasmus wrote:
On Wed, 18 Jun 2008 23:07:40 +0200, Jeff <jeff@spam_me_not.comwrote:
>I had this little bit of code:

function parseTemplateData($file){
$file2=preg_replace_callback(
"/<edit(.*?)>/s",
"parseEditTag",
$file);
echo $file2;
}

and that did what I needed, so I thought I'll put it in a class:

class FOO{

public function parseTemplateData($file){
$file2=preg_replace_callback(
"/<edit(.*?)>/s",
"parseEditTag",
$file);
echo $file2;
}

public function parseEditTag($tag_content){
...

}

Valid callbacks are:
$callback = 'function_name';
$callback = array($instanceofObject,'method_name');
$callback = array('ClassName','static_method_name'));

If you went to the manual for preg_replace_callback:
http://nl2.php.net/preg_replace_callback
..and clicked on 'callback', you end up here:

http://nl2.php.net/manual/en/languag...types.callback
Thanks for the following example, it's perfectly clear.

And since the next thing on my list to learn was "call_user_func", I've
already learned that with the 'callback' lesson!

Jeff
>

<?php
class Foo{
function test(){
echo preg_replace_callback('/a/',array($this,'funcname'),"acac");
echo preg_replace_callback('/a/',array('Bar','funcname'),"acac");
echo preg_replace_callback('/a/','funcname',"acacacaca");
}
function funcname(){
return 'b';
}
}
class Bar{
static function funcname(){
return 'd';
}
}
function funcname(){
return 'e';
}
$f = new Foo();
$f->test();
?>
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Phil Powell | last post by:
My first time working with a PHP class, and after 6 hours of working out the kinks I am unable to return a value from the class, so now I appeal to the general audience what on earth did I do wrong...
5
by: Murat Tasan | last post by:
so i have a situation that i know cannot be solved directly, and i've already worked around it, but i was hoping to find an explanation as to why this behavior is as it is... i have an abstract...
8
by: DaKoadMunky | last post by:
Please consider the following... <CODE> #include <string> using namespace std; typedef int PrimitiveType; typedef string ClassType;
9
by: Rob. | last post by:
I originally thought this was a compiler error but it seems the standard prohibits the code below. Has anyone got any good ideas about how to code around it? class A; class B; class X; class...
6
by: Busin | last post by:
Classes B, C, D and E are derived from base class A. A* CreateB(); A* CreateC(); A* CreateD(); A* CreateE(); class X { public:
12
by: Michael Maes | last post by:
Hello, I have a BaseClass and many Classes which all inherit (directly) from the BaseClass. One of the functions in the BaseClass is to (de)serialize the (inherited) Class to/from disk. ...
7
by: nafri | last post by:
hello all, I want to create a function that returns the first element of the Array that is input to it. However, the Input Array can be an Array of points, double, or anyother type, which means...
8
by: WakeBdr | last post by:
I'm writing a class that will query a database for some data and return the result to the caller. I need to be able to return the result of the query in several different ways: list, xml,...
46
by: Steven T. Hatton | last post by:
I just read §2.11.3 of D&E, and I have to say, I'm quite puzzled by what it says. http://java.sun.com/docs/books/tutorial/essential/concurrency/syncrgb.html <shrug> -- NOUN:1. Money or...
10
by: Raj | last post by:
I need a VB function to return array of collections like Private Type Employee empname as string address as string salary as integer deptno as integer End Type dim employees() as Employee
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.