Solved

Friday Fun

Posted on 2002-06-27
8
177 Views
Last Modified: 2008-02-20
Right then lads, here's an interesting bit of script.

Don't worry yourselves with the variable variables, questionable coding standards, and redundant code...

There's 400 easy points up for grabs if anyone wants them:

1. what does this code do?
2. how does it do it?
3. how might the class be used in building other classes?
4. can you think of any applications it might be useful for?
5. Is their a built in php funcion that can achieve a similar result?
6. How is it different and when/why would you choose one over the other?

class theClass
{

     var $classname  = "theClass";
       var $pieces = array("classname");


     function toFoo()
     {

          $str="";
          $this->daMainBit("this", $str);
          return $str;
     }

     function yourHavinALaugh(&$str)
     {

          eval(sprintf(";%s",$str));
     }


     function daMainBit($prefix, &$str)
     {

          static $t,$l,$k;

          eval("\$t = gettype(\$$prefix);");

          switch ( $t )
          {

               case "array":

                    eval("reset(\$$prefix); \$l = gettype(list(\$k)=each(\$$prefix));");

                    $str .= "\$$prefix = array(); ";

                    while ( "array" == $l )
                    {
                         $this->daMainBit($prefix."['".ereg_replace("([\\'])", "\\\\1", $k)."']", $str);
                         eval("\$l = gettype(list(\$k)=each(\$$prefix));");
                    }
                    break;

               case "object":
                    eval("\$k = \$${prefix}->classname; \$l = reset(\$${prefix}->pieces);");
                    $str.="\$$prefix = new $k; ";
                    while ( $l )
                    {
                         $this->daMainBit($prefix."->".$l,$str);
                         eval("\$l = next(\$${prefix}->pieces);");
                    }
                    break;

               default:
                    eval("\$l = \$$prefix;");
                    $str.="\$$prefix = '".ereg_replace("([\\'])", "\\\\1", $l)."'; ";
                    break;
          }
     }
}
?>


0
Comment
Question by:dht1
  • 4
  • 4
8 Comments
 
LVL 40

Expert Comment

by:RQuadling
ID: 7115993
It looks like a class that can describe itself.

Unfortunately, I don't have enough class knowledge to know for sure.

I tried ...

$stuff = new theClass;
echo $stuff->toFoo();

and got ...

$this = new theClass; $this->classname = 'theClass';

I've no idea what the yourhavinalaugh() function does. I think it tries to interpret a string it was passed by reference. But not stuff I've done.

That's it. No idea!

Ha!

Richard.
0
 

Author Comment

by:dht1
ID: 7117388
Yeah, it's certainly reflective... arguably recursive.

It could be considered asexual ie it can reproduce itself.

1 down, 5 to go.

C'mon guys, it's not that hard.
0
 
LVL 40

Expert Comment

by:RQuadling
ID: 7121268
My problem is I don't use classes yet. My scripts are relatively simple.

If a new class was designed with this as the base class (can that be done?) then would the new class then be self-descriptive?

How would you add a new class to the $pieces array OUTSIDE of the class? (Add, not replace)?

Would the toFoo() function then describe the other classes?

A clue would be nice as I really don't know enough about classes!

Richard.
0
 

Author Comment

by:dht1
ID: 7123070
A new class could theoretically be designed with this as the base class - that would be an extends or implements relationship, and this wouldn't really be the way to go about doing it. Perhaps you're talking about inheritance...

Suppose you're working on something to the n'th tier. In that instance, assume your object is a level in the hierarchy. You don't know how many objects you're going to need to create.

A good place to start on OO is a tutorial on DevShed.com called "Back to class". It should get you into the swing of it fairly quickly.

The only comment I would make is that in very broad terms, if you're needing to code object-orientedly, you're probably better off with a language such as JSP.

Anyway have a browse through the Devshed tutorial, and perhaps search the web for stuff on OO programming (such as inheritance) - it should be an interesting read, and a whole new approach to development. You'll either love it or hate it.

David
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 40

Accepted Solution

by:
RQuadling earned 400 total points
ID: 7124063
I've done some inheritance in Delphi, but only because it seemed the obvious way to do it and it took me an age to get it right! But now I have a generic maintenance form linked to any database and specific forms, coming from the general form. If I add new base functionality to the general form, all forms based on this form get the new functionality.

Thanks for the pointers though (ha pointers!!).

Richard.
0
 

Author Comment

by:dht1
ID: 7130654
Have some points for self-improvement... or summat!
0
 
LVL 40

Expert Comment

by:RQuadling
ID: 7131380
Thank you.

What DOES the class actually do?
0
 

Author Comment

by:dht1
ID: 7131533
yourHavinALaugh doesn't do anything at all - it's just there for the hell of it.

The class basically allows you to reproduce more of the same objects as and when required.

It doesn't have any distinct purpose in its current state.

A good place to use it may be if you're writing a debugger.
0

Featured Post

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now