Discover the best of the web!
Learn more about Digg by taking the tour.
Dynamic PHP - Tricks PHP can do that Java/C#/C++ wont
128.ibm.com — Creating dynamic classes that change themselves at run time and creating new methods and member variables on the fly. You can't do that with the Java, C++, or C# languages.
- 921 diggs
- digg it
- SilverRocket, on 10/12/2007, -0/+0You know what? These IBM articles are pretty well written. With a bit of OOP knowledge, articles like this can take a PHP programmer to the 'next level' so to speak.
- ravuya, on 10/12/2007, -1/+2But you *can* do it with lisp or python. And there are lots of things that lisp can do that PHP still won't.
It's a well written article, but I'd be surprised if I'd ever see a PHP jockey using this kind of stuff. - joeolivas, on 10/12/2007, -1/+3Learn Lisp. Then realize the shortcomings of every other language in existance.
- eridius, on 10/12/2007, -0/+1You can do it with ruby too. I basically skimmed the article, but going by the Digg description you can even do this with a compiled language like, say, Objective-C. Well, you can add new methods at least. Adding new ivars could technically be done but would kinda screw with subclasses and it's a wee bit difficult to access said dynamic ivars. But you could mimic it with a single NSDictionary ivar, and you can certainly add dynamic methods or even handle unknown methods.
- SpookyET, on 10/12/2007, -3/+5What a load of BOLLOCKS! You can emit code in .NET in any language at runtime, and it works similar to this. You may be able to emit extension methods too.
RUBY supports this with a normal syntax, not those "call" hacks. That PHP code is ugly as hell and very complicated. No DIGG. - MrDiaz, on 10/12/2007, -0/+0The Author is an actual PRO. We can tell just by reading.
- egbert, on 10/12/2007, -0/+1Nothing new. Lisp, Python, Ruby, ... can all do this.
- justinp, on 10/12/2007, -0/+0Wasn't this on Slashdot a day or two ago?
- opera, on 10/12/2007, -3/+2PHP kicks ass? No. PHP sucks. The language is extremely ugly, it still lacks decent open development suits, all functions and features are added over the years and today it's just a complete mess of junk. It used to be powerful, but now it's a bloat. What the h*ll is that dollar sign doing there in the first place? It's just stupid.
This is probably why client side scripting (I really don't like the word ajax which doesn't mean anything) have grown so large. People (like me) have grown very sick and tired of PHP, and javacsript/ecmascript is soooo much nicer.
And btw, Java, C++ or C# aren't scripting languages last time I checked. - officialchicken, on 10/12/2007, -1/+5LAME
"but you can also create objects that bend at runtime, creating new methods and member variables on the fly. You can't do that with the Java, C++, or C# languages"
This is a lie. - eqbridges, on 10/12/2007, -0/+2absolutely bogus.
check out http://jakarta.apache.org/bcel/ - augsod, on 10/12/2007, -0/+2officialchiken is right. C#, and any .NET language for that matter, has reflective capabilities far more powerful than that of Python. Anything that you can do in code at compile time can be done at runtime (usually with a good performance boost too) and you can even compile and load assemblies into your program from code dynamically (although you cannot unload an assembly if it is in the same appdomain as the main program).
- Valence, on 10/12/2007, -0/+0This is a little goofy. I mean, nice article, with special relevance to those working with legacy systems and in heavy Java environments, where prolonged exposure to statically typed languages has created an unsatisfied yearning for this kind of solution.
First of all, the statement's false. Strategy pattern. Done. Yay.
Secondly: Lisp, Python, Ruby. Done. Yay.
But it's a nice article, and if you're in the PHP world, then bully for you. - oepapel, on 10/12/2007, -1/+2"This is a lie."
I completely agree. It is absolutely possible for ANY managed language to dynamically disassemble, reassemble, generate or otherwise create new or modified objects on the fly. And since the compiler is part of the framework, you can compile those new classes so there is no interpreter penalty.
Actually, a giant advantage that managed languages have over PHP is the concept of interfaces which negates the need to massage the objects in the first place. - drawkbox, on 10/12/2007, -0/+0PHP5 is good, all platforms are good really because they have survivied the scrutiny but .NET C#, VB.NET can create dynamic classes with System.CodeDOM, dynamic compilation and then loading with reflection. With Generics this makes entity creation easy and very pluggable and dynamic. THere are different ways to do things in every platform but dynamic classes are possible in .NET and really Java as well with a good enough programmer.
The article is basically "selling" rather than the straight fact. No programmer should be limited by what a langauge can and can't do, but stricctly knowing languages tricks and limitations in building your master plans muhahahahhhah .. muahahhaa. muah. mhm. - da5id, on 10/12/2007, -0/+0WOW, I'm speechless!
- CaughtThinking, on 10/12/2007, -0/+0Just remember the point of Java/C#/C++ is to NOT do these things :)
The IBM articles are great though, it's extremely rare they don't provide you with some practical advice. - verucasalt, on 10/12/2007, -0/+2Uh, you cant do this in C#?
Thats news to me... I just did it earlier in the week.
And by the way... just because you CAN do something doesn't mean you SHOULD. I can only imagine very rare circumstances where this is actually a GOOD idea. - keithgabryelski, on 10/12/2007, -0/+1The article is nice introduction to class manipulation in PHP, but the author didn't do his homework.
C# provides class manipulation through reflection AND it is type safe -- this provides benefits
PHP can not.
Yes, PHP provides other benefits that C# doesn't -- choose as you will. I actually like the syntax
foreach (someArray as key => value) { ... }
but C# is still my favorite. - jayf, on 10/12/2007, -3/+5This is one reason I like Java actually.
Many of these web languages (yes I know PHP can do EXEs too) and wacky features made for them are designed for non-programmers (fighting the temptation not to call them script kiddies) who don't have the patience to sit down and actually "think out, grasp and understand" something properly in order to design it perfectly. If you have a class that needs redefining on the fly it means you didn't design it well enough in the first place. Normally I wouldn't be bothered by such an anomaly as this but the people who preach these languages are beginning to make an impact on the industry and I fear for the worse.
I'm amazed by what has happened in the last 5 years. The ratio of actual programmers and developers (people who love it enough to do it on their own free time) to non-programmers (people who used to work in a record store and saw an opening for a programmer in the paper and went for it) is falling rapidly. Add designers who think knowing Actionscript makes them developers to the mix and you see the problem. These people are amateurs with no real understanding of the inner workings of computers or the true art of development.
If this were the shoe business it would be like comparing an 80 year old Italian man who has made shoes since he was 12 and loves and understands the art and mechanics of feet to a recent hire at Nike's Manila factory making that plastic bubble that goes into every shoe with the word "air" on it. What they do is deceptively similar on the surface but digg deeper and you see the difference. One is art, a true devotion to understanding every aspect of the development process, the other is a mass-produced process where nobodies off the street instant foot smiths.
I am a member of the former. What about you? - jmcmunn, on 10/12/2007, -0/+1
Come on, this is front page news? It's not even true. You can do this with any of the new .Net languages (or probably with other managed languages). But seriously, stop digging this crap. At least research something before you open your mouth. - officialchicken, on 10/12/2007, -0/+1"Just remember the point of Java/C#/C++ is to NOT do these things :) "
Acutally, C# was designed to do these things from the beginning since it did not support anonymous methods in 1.0. Java added similar metadata functionality at a later date. - officialchicken, on 10/12/2007, -0/+0I forgot to add, both had reflection from the beginning.
- KayMan2k, on 10/12/2007, -0/+1C# / .Net is perfectly capable of doing these 'tricks' (aka run-time class creation). But why do you even need the overhead of a dynamically created class when a "normal" class with a parameters array would work just the same? Heck, .Net offers so many code generation tools that you could just describe your data and not write a single line of code.
- solarpowered, on 10/12/2007, -0/+1"You can't do that with the Java, C++, or C# languages"
You can with Smalltalk, since the early 80s.
Wow, the more things change, the more they stay the same. - TheQwe, on 10/12/2007, -2/+0python > php
'nuff said. - officialchicken, on 10/12/2007, -0/+1"C# / .Net is perfectly capable of doing these 'tricks' (aka run-time class creation). But why do you even need the overhead of a dynamically created class when a "normal" class with a parameters array would work just the same?"
Sometimes applications have to take into account that they may be consuming another API or datasource which will or can change on the fly. Database object mappers come to mind, and creating an 'engine' for your own interpreted scripting language is another case where trying to hardcode a class or method for every possible permutation is downright ignorant. - mattclare, on 10/12/2007, -2/+0Here's something you can't do with Java or C++ or C# -- generate a web page with seven key strokes:
- mattclare, on 10/12/2007, -1/+0< ? P H P ? >
- MioTheGreat, on 10/12/2007, -1/+0Lies. All lies. C# can....It's just a resource-hungry thing to do.
- ElectricGrandpa, on 10/12/2007, -0/+1"Add designers who think knowing Actionscript makes them developers to the mix and you see the problem. These people are amateurs with no real understanding of the inner workings of computers or the true art of development."
So you want being a developer/programmer to become a closed field, where you can't do it unless you've been doing it your whole life? It doesn't make any sense. You're way too jaded by your own superiority. I'm a professional actionscript developer, and while I don't have a deep knowledge of the "inner workings of computers" or the "true art of development", I do make some damn good stuff that works well and makes my employers happy. I do innovate and problem solve and come up with interesting solutions every day. I may not be involved in the lifelong zen of programming, but I enjoy it. - oepapel, on 10/12/2007, -1/+0"Here's something you can't do with Java or C++ or C# -- generate a web page with seven key strokes:"
That's one damn useful page. If we are talking about generating blank content, I can generate a blank post with NO key strokes. - energeek, on 10/12/2007, -0/+0"Many of these web languages (yes I know PHP can do EXEs too) and wacky features made for them are designed for non-programmers (fighting the temptation not to call them script kiddies) who don't have the patience to sit down and actually "think out, grasp and understand" something properly in order to design it perfectly. If you have a class that needs redefining on the fly it means you didn't design it well enough in the first place. Normally I wouldn't be bothered by such an anomaly as this but the people who preach these languages are beginning to make an impact on the industry and I fear for the worse."
I agree with you on the fact that many non programmers hop on the bandwagon for the cool factor. But you can't really blame the languages. They can be used for a lot of serious stuff. Its not the language's fault its the SKs fault. I mean I am 15, I love as in absolutely love programming, and still I like PHP and Python just because Python is as close to pseudocode as you can get, and its fun and it allows you to learn a lot. I know Java and C++ too but even you will agree that you like languages that allow you to run code without the compile step in between :) - oepapel, on 10/12/2007, -1/+0"I'm a professional actionscript developer, and while I don't have a deep knowledge of the "inner workings of computers" or the "true art of development", I do make some damn good stuff that works well and makes my employers happy"
That's great. The fact that you admit to not having a knowledge of the inner workings of computers is what keeps you from being a developer. At least a good one. Now, don't get me wrong. It's not the script thing. I know plenty of "armchair" programmers or "weekend" programmers that can muddle through their jobs and get satisfactory evaluations. But they are also the first ones to come to me when their development computer breaks down or is acting wonky. It truly is sad when a programmer can't maintain their own system. It's like a professional mechanic that gets a gas attendant to check his oil. - narzy, on 10/12/2007, -0/+0screw all that, it's all about assembly, hell if we wanted to be most effeciant we would design custom circuts for each application.
- oepapel, on 10/12/2007, -2/+0"screw all that, it's all about assembly, hell if we wanted to be most effeciant we would design custom circuts for each application."
what do you think an iPod is? Or a PSP? Or an XBOX 360? Or a TiVO?
BTW, all the above are programmed in "C" - nocotigo, on 10/12/2007, -0/+0The example does what Propel does for PHP already. I know it was just the means to explain the PHP concepts; I just thought I'd mention it. http://http://propel.phpdb.org/trac/
- Inkling, on 10/12/2007, -1/+1The real question is "Why would you want to?". Just because you can do something, doesn't mean you should. C# and Java are correctly designed so you don't need to do this. It just isn't good programming practice to do this and it's wasteful on performance and maintenance.
This is a perfect example of why PHP programmers have such trouble creating robust and scalable enterprise applications. Sure PHP programmers are great at creating shopping carts, pretty websites, and other old tech, but they just don't understand good program design.
Further, they know that PHP is a poor programming language which is why they feel compelled to submit these articles trying to justify their language's existence. - nocotigo, on 10/12/2007, -0/+1I hate when people invoke the words "robust and scalable," and somehow think that just by saying them, they've made an argument.
- digx, on 10/12/2007, -0/+1You can absolutely do this in C#...
- lofye, on 10/12/2007, -0/+0I'm a "PHP Jockey" not because I believe it's the best scripting language, but because it has been the cheapest to implement for the longest amount of time, and it has a huge developer community. The server cost to the client is NIL, which is not the cast for .NET, ColdFusion, or JSP. For small to medium sized businesses PHP is perfect. Sure, Enterprises *might* want something more, but it's good enough for Yahoo! And yes, us jockeys do occasionally whip out the big dynamic object/method schlong.
- officialchicken, on 10/12/2007, -0/+0"screw all that, it's all about assembly, hell if we wanted to be most effeciant we would design custom circuts for each application"
FPGA all the way!!! - SirNuke, on 10/12/2007, -0/+1When exactly would you use Java, C++, or C# in place of PHP?
- reikiman, on 10/12/2007, -0/+1I blogged my response here: http://weblogs.java.net/blog/robogeek/archive/2006/02/bendy_classes_a.html
I made two points:
a) Java with Annotations offers a fourth solution the guy didn't even present as a possibility.
b) Compile time checking makes it easier to catch bugs due to code mismatches. By dynamically generating object interfaces you push catching programming errors to runtime, which means more bugs will slip through your testing and show up in the field.
- futureundead, on 10/12/2007, -2/+0re: augsod
"C#, and any .NET language for that matter, has reflective capabilities far more powerful than that of Python."
Really? More powerful than Python? Goodness, I'd like to see that. Let's add methods to an instance in Python, given some source in a string...
import compiler
import new
code = compiler.compile(source, "module_name", "exec")
a = {}; eval(code, a, a)
for name, obj in a.iteritems():
....if not callable(obj): continue
....setattr(instance, name, new.instancemethod(obj, instance, instance.__class__))
Do that in C#. - ph713, on 10/12/2007, -0/+0"But you *can* do it with lisp or python. And there are lots of things that lisp can do that PHP still won't.
It's a well written article, but I'd be surprised if I'd ever see a PHP jockey using this kind of stuff."
You also can do this kind of stuff (and even far, far, scarier stuff) in Perl, and people actually regularly use published modules that make use of these kinds of tricks.
If you ever want to get into truly hairy insane OO design patterns (which are also neccesary to work really insane magic in a module to make the end-coder's life easier), try looking at some of the more recent work in OO perl modules.
The reason perl goes even further than the "normal" tricks you see in other dynamic languages is mostly due to two things above and beyond the usual "I can eval strings as code"-based tricks:
1 - We can directly modify the symbol tables of the interpreter without any regard for our own safety (when we explicitly locally turn off the usual warnings about such things), in cases where string evals simply aren't cool enough.
2 - We have "source filters", which allow one to dynamically modify the very syntax of the language itself. That doesn't mean just new keywords, that means whole new incompatible syntax structure. For a touch-in-cheek example of the power of this, check out http://search.cpan.org/~dconway/Lingua-Romana-Perligata-0.50/lib/Lingua/Romana/Perligata.pm
which is a module that allows your perl program to be written in syntactically, grammatically correct Latin prose, with no other funny decorations. There are practical uses of course, where people have used it to, for instance, import the try/catch syntax of a foreign programming language into perl's syntax just because they like it: http://search.cpan.org/~nilsonsfj/Error-TryCatch-0.05/lib/Error/TryCatch.pm
Combine those with the tricks you see in the python article, and you get amazing and easily abused power. - oepapel, on 10/12/2007, -0/+0"When exactly would you use Java, C++, or C# in place of PHP?"
When developing/deploying an ASP.net solution instead of a PHP solution. - officialchicken, on 10/12/2007, -0/+0When exactly would you use Java, C++, or C# in place of PHP?
Four, that I've worked on come to mind
1. Embedded systems
2. Legacy system wrapper
3. Autonomous agent-based systems
4. Graphics (OpenGL) application - futureundead, on 10/12/2007, -1/+0re: reikiman
Compile-time checking only makes sure that your code is type-safe. Type-safety is nice, but it's about as useful as making sure that the cookies you are making contain flour, sugar, eggs, butter, etc. Actually making the code *work* (or the cookies are tasty) is something that your compiler can't do...which makes your compile-time checking worth very little. - oepapel, on 10/12/2007, -0/+0"Really? More powerful than Python? Goodness, I'd like to see that. Let's add methods to an instance in Python, given some source in a string..."
Wow! Alarms going off in my head! Code insertion security vulnerability detected. This code would never be allowed in a production environment because even the most incompetant hacker could exploit it.
I'm glad that this security breach was so easy for you to generate... -
Show 51 - 94 of 94 discussions

