Solved

Friday Fun

Posted on 2002-06-27
8
178 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
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 …

920 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

17 Experts available now in Live!

Get 1:1 Help Now