Page 1 of 2

methods and abstraction [help me with this title rofl]

Posted: Wed Jan 17, 2007 3:41 pm
by daedalus__
I have a lot of methods in my templates that have names such as:

AddJavascript
AddStyle
AddHeader
AddMeta

I have corresponding methods for parsing these things:

ParseJavascript
ParseStyle
ParseHeader
ParseMeta

When it comes time to parse the arrays that hold the data and turn them into blocks to be inserted into the template to create the view i do not want to have to type these things out 800000000 times so I want to do something like:

Code: Select all

$methods = findAllMethodsNamedParse();

foreach ($methods as $info)
{
    $info['obj']->$info['method']();
}
Would someone care to describe or illustrate a good way to do this? I will post code if you'd like.

Thanks

Posted: Wed Jan 17, 2007 3:49 pm
by feyd
Create a method in the class that can call all of them.

Posted: Wed Jan 17, 2007 4:16 pm
by daedalus__
Boring :(

lol

Re: methods and abstraction [help me with this title rofl]

Posted: Wed Jan 17, 2007 5:12 pm
by Christopher
I'm with feyd, how about:

Code: Select all

function parse () {
     $this->ParseJavascript();
     $this->ParseStyle();
     $this->ParseHeader();
     $this->ParseMeta();
}

Posted: Wed Jan 17, 2007 5:15 pm
by Luke
daedalus... you look exactly like my buddy russell olson :?

Posted: Wed Jan 17, 2007 5:45 pm
by Maugrim_The_Reaper
I think this works too of you want to be a real smartass ;).

Code: Select all

function parse() {
    $funcs = array('Javascript', 'Style', 'Header', 'Meta');
    foreach($funcs as $post)
    {
        $func = 'Parse' . $post;
        $this->{$func}();
    }
}
Is that less boring? :)

Posted: Wed Jan 17, 2007 8:26 pm
by Ambush Commander
I'm sure you could achieve something to the original posters desire with reflection...

Posted: Wed Jan 17, 2007 9:53 pm
by feyd
Ambush Commander wrote:I'm sure you could achieve something to the original posters desire with reflection...
Yeah, it's certainly possible with reflection, but since PHP 5 wasn't specified I assumed 4.

Posted: Thu Jan 18, 2007 1:35 am
by daedalus__
The Ninja Space Goat wrote:daedalus... you look exactly like my buddy russell olson :?
rofl. isn't that image sweet?
Maugrim_The_Reaper wrote:Is that less boring? Smile
Yes!
feyd wrote:but since PHP 5 wasn't specified I assumed 4.
PHP 5.2.0

Posted: Thu Jan 18, 2007 7:39 am
by feyd
If you want to look into reflection then ReflectionClass should be of interest.

Posted: Thu Jan 18, 2007 7:52 am
by sike
feyd wrote:If you want to look into reflection then ReflectionClass should be of interest.
boring!

here's the most natural tool to use http://www.php.net/manual/en/ref.tokenizer.php

Posted: Thu Jan 18, 2007 10:14 am
by Jenk
"boring" is hardly a justifiable reason to ignore a possibly more efficient method.

Reflection is also more 'fun' in my opinion.. unless you like spending ages tweaking a parser.. (btw - just how would tokenizer work in this scenario?)

Posted: Thu Jan 18, 2007 10:31 am
by sike
Jenk wrote:"boring" is hardly a justifiable reason to ignore a possibly more efficient method.

Reflection is also more 'fun' in my opinion.. unless you like spending ages tweaking a parser.. (btw - just how would tokenizer work in this scenario?)
hehe... it was meant as a joke - in real live i would never ever touch a tokenizer for this type of "problems"

Posted: Thu Jan 18, 2007 12:35 pm
by daedalus__
Daedalus loves reflections of all kinds. He especially likes reflections on water.

fastest way to find string?

Posted: Fri Jan 19, 2007 4:51 pm
by daedalus__
What would be the fastest way to find the string "parse" in another string? String functions or Regular Expressions? Might one string function be faster than another?

Thanks.