Solved

Friday Fun

Posted on 2002-06-27
8
181 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How do I post more than 1 item to php backend 24 60
geting data from the array list 6 38
Testrail - Active Directory integration. 4 35
Link Stopped Working 7 32
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

751 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