methods and abstraction [help me with this title rofl]

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

methods and abstraction [help me with this title rofl]

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Create a method in the class that can call all of them.
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

Boring :(

lol
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

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

Post by Christopher »

I'm with feyd, how about:

Code: Select all

function parse () {
     $this->ParseJavascript();
     $this->ParseStyle();
     $this->ParseHeader();
     $this->ParseMeta();
}
(#10850)
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

daedalus... you look exactly like my buddy russell olson :?
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post 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? :)
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

I'm sure you could achieve something to the original posters desire with reflection...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

If you want to look into reflection then ReflectionClass should be of interest.
sike
Forum Commoner
Posts: 84
Joined: Wed Aug 02, 2006 8:33 am

Post 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
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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?)
sike
Forum Commoner
Posts: 84
Joined: Wed Aug 02, 2006 8:33 am

Post 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"
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

Daedalus loves reflections of all kinds. He especially likes reflections on water.
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

fastest way to find string?

Post 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.
Post Reply