Solved

Friday Fun

Posted on 2002-06-27
8
182 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

 

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 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: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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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…
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

624 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