Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Friday Fun

Posted on 2002-06-27
8
Medium Priority
?
185 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:Richard Quadling
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:Richard Quadling
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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
 
LVL 40

Accepted Solution

by:
Richard Quadling earned 1600 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:Richard Quadling
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…
Suggested Courses

972 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