PHP 5 OO In 24 Slides
An anonymous reader writes "At php|works about a week ago John Coggeshall gave an interesting talk on object oriented programming using PHP version 5. Even if you couldn't be at the talk, his slides are available online which gives you a pretty good idea of what you can expect from the OO model. Java programmers will be pleased."
From slide 5:
Member methods may also be declared final, preventing them specifically from being overloaded in a sub class
s/overloaded/overridden/
See here.
The only languages with semi-decent OO are ones with multimethods/generic functions e.g. Common Lisp.
Oh great. A Lisp zealot and an OO zealot all in one person. Such will probably trigger a singularity and end civalization. Much more toxic than even the mod points I am surely about to receive.
It really depends on what you are trying to achieve but I used PHP CLI many times to solve quick problems. PHP's impressive function library makes it easy to do anything. Can you give me an example where PHP isn't suitable for general scripting?
BTW, I agree PHP is better at web than scripting. Now back to the main topic...
As always, Coggle (I just need to spread John's 'short' name) used his PHP girls in the slides.
These are far from being the first slides about PHP 5 available. I had seen some from Zeev Suraski 1.5 year ago. Actually, about every PHP conference around the world ever since PHP5 was buildable had it's set of PHP5 slides.
Qui ne va pas à la chasse n'a pas de gibier
PHP Queb
As many times as I've heard this fun fact, it never ceases to amaze me: "In ZE / PHP 4 objects weren't references, so when they were passed to a function a copy was made."
I've never heard this one before, and it's just as ridiculous: "In PHP 4, you couldn't access a member function or properity indirectly through another object's member function or properity.."
Yikes. How did they get it that wrong!?? And how did they manage to come up with (what looks like) a very reasonable object system for PHP 5?
Also, what is the "interface" feature for, if the language is not statically typed? Will you get a runtime error as soon as an implementing but non-conforming class is defined? Or is it primarily just a more formal way of documenting your class? Or does it work solely in concert with type hinting to prevent you from passing in an incorrect type?
What's wrong with a standard toolkit of 1500 separate classes?
If you bother to make slides for a presentation, and then publish them on the web, you should take the extra minute or two and run a spell check over them. Reading something that includes misspellings and grammatical errors every other sentence is not just distracting -- it makes me wonder if the content and examples are correct.
Personally, I'm less than impressed. Why should I cheer and hooray about PHP introducing some OO features in PHP in 2004 almost all of which Java has had since version 1.0 in 1995? Is that supposed to be a great feat?!
Stupidity is mis-underestimated.
Several things.
1)Its too big. With 1500 classes there's no way a developer can keep a significant chunk in his mind
2)Its too big part 2- the bigger the library, the more bugs it has
3)Having one giant library that does 10 billion things isn't a good idea. Its better to have individual libraries written, and have more focus on those libraries by the teams. Huge projects like Java's framework end up producing subpar code
4)It kills innovation. In languages without large standard libraries, implementations compete and only the best get common usage. With 1 standard huge library, people limp along with the standard so as not to reinvent the wheel. This means good ideas aren't followed through and written, and huge losses in productivity
5)Backwards compatibility. When you have a standard library, those methods need to be supported forever. When its third party libraries, the third party will support them, and the language can evolve without worrying. If they aren't supported a new supported library will rise throught he competition
I still have more fans than freaks. WTF is wrong with you people?
Does it make sense to define classes, create objects, restore objects from some serialized storage or database, etc. etc. in the context of a web application? That's a lot of scaffolding to build and tear down for every HTTP request.
OOP clearly makes sense in many applications where the class definitions and objects have a lifetime longer than a single call. And it's possible to write server-side applications that "live" longer than a single HTTP request. But that isn't the PHP execution model.
The ZEND Accelerator has clever caching and compilation mechanisms, but how many people have the Accelerator installed? How many ISPs? Without those optimizations the PHP interpreter is scanning/compiling the entire script (including all of class definitions) for each request. In that environment it makes sense to keep the scripts as short and simple as possible, not add code and complexity with OOP.
I'm not saying that OOP is a bad idea, even in PHP code. But does it make sense to implement full-blown OOP instead of using included files for common code? In my PHP 4 projects I mainly used classes to get a namespace, not for any other OOP purposes.
1)Its too big. With 1500 classes there's no way a developer can keep a significant chunk in his mind
In what way is a standard library of 1500 classes, all documented in the same place, harder for developers to understand than 200 different libraries each implementing 10-15 classes?
2)Its too big part 2- the bigger the library, the more bugs it has
Of course 1500 classes are likely to contain more bugs than 15 classes. But do you have any evidence that those classes are likely to be buggier as a result of being standardised, than they would have been if they'd been written by dozens of different people and never tested together?
3)Having one giant library that does 10 billion things isn't a good idea. Its better to have individual libraries written, and have more focus on those libraries by the teams.
Do you have any evidence that it's better for the teams who write these 1500 classes NOT to talk to one another, standardise their design, and test their classes together, than it is for them to collaborate?
4)It kills innovation. In languages without large standard libraries, implementations compete and only the best get common usage. With 1 standard huge library, people limp along with the standard so as not to reinvent the wheel. This means good ideas aren't followed through and written, and huge losses in productivity
This is the only one of your points that has merit, and even there it's unclear. I'll put it this way: I am more likely to use advanced functionality in a language where that functionality is standard (and therefore present on everyone's computer) than I am in one where it is not.
Concrete example: when I'm developing in Java, I use the cross-platform Swing library, because it comes with the language. When I'm developing in C#, I write the GUI with Windows.Forms, even though I know that I could be using GTK# and letting Linux users use my application. Why do I do that? Because Windows users are more numerous than Linux users, and Windows users are not willing to download ten megabytes of GTK+ runtime to run my program.
If the GUI library had been part of the actual CLR standard, then it would have been easily implementable on Linux, and the problem would not have arisen. But as the most popular GUI library for C# is NOT part of the standard library, it is not available everywhere, and Linux users suffer the consequences.
5)Backwards compatibility. When you have a standard library, those methods need to be supported forever. When its third party libraries, the third party will support them, and the language can evolve without worrying. If they aren't supported a new supported library will rise throught he competition
Which is great - until YOU find yourself wanting to use some old code that was written for a third-party library that was discontinued years ago. Small comfort that there's a new better library to frobnicate widgets, when you're faced with having to waste a month converting 200,000 lines of code to USE this new library, or installing Windows 98 on an old machine to use the only version of the old library you can find!
It comes down to this -- small standard libraries and fast-changing implementations are fine in their place, but that place is the hacker world, where people learn new APIs for fun, and rewrite all their code every day before breakfast. In the real world, where Java (and PHP) are being used, the important thing is STABILITY. We need to know that the code we write today will be reusable in five years' time. And your cool innovative evolving libraries do not even pretend to provide that guarantee.
Java programmers will be pleased.
I'll be pleased when PHP gets static typing.
1. Classes are encapsulations of logic (variables and functions). What was once a shared library on its own can be distilled to one or a few classes. If each class had about five methods (functions) and two static properties (variables), the equivalent code body would be 7,500 functions and 3,000 global variables. Since classes/objects (and packages!) keep similar functionality compartmentalized, it's actually easier to keep a significant chunk in mind. Interfaces (method definitions without implementation) make it even easier. Personally I haven't had a problem keeping the packages I use on a project straight in my mind. But then, maybe I'm just smarter than you.
...to innovate? Innovation comes when you don't have to reinvent the wheel and can concentrate on the task at hand (which probably isn't determining the next new hashtable implementation).
2. The smaller the library, the less it does. What's your point? At the end of the day, when your project (without objects or a complete standard library) is finally ready, you have linked to quite a few shared libraries, reinvented the wheel twice, and have very little standardized that would be recognized by the next person who comes to maintain the project.
The more the standard library does, the less code (with bugs) you have to write and maintain. The great part about the standard library is that since everyone is using it, everyone's finding and fixing the bugs together. That said, the Java standard library has been extremely solid for me. What bugs have you run into habitually? You do have some examples right?
There is no requirement that a developer use all features of a standard library any more than it's required that a developer use every interface defined by POSIX in every project. But in both cases, it's nice to know the standard interface for your next project is there.
3. Yes, better to compartmentalize items. Make separate components. Put them in... ummm... PACKAGES. Yeah, that's it. That way I can keep my general utilities and basic data structures separate from my I/O routines which are kept away from my database layer and my GUI and... java.util, java.io, java.sql, java.awt, etc. So what's the problem?
Huge projects like C's standard library and the hodgepodge of 3rd party libraries end up producing subpar code. Your point?
4. Yes! Because Java hasn't changed in the last ten years at all. Pray tell, what innovations were you speaking of? The innovation of yet another person making yet another linked list or hashtable tailored exactly for their needs? (Wee! This keeps a specific type of object in 1KB less per thousand objects than the standard! I am 7331!) You want to talk about innovation? C and C++ coders couldn't get together to decent database abstraction layer. ODBC (although more specifically the drivers written for that vague standard) is such a piece of crap, people started actually thinking datasource abstraction was a bad idea. What about the fact that few C or C++ (or LISP or Perl...) coders bothered to make documentation at all let alone generate it from the source code itself until javadoc came along. Now it's all "we have that too!"
If you're spending all your time reinventing the wheel, when do you have time to make something new?
5. Nothing says innovation quite like "Fuck! The library changed interfaces again. Alright, let's go back, fix stuff, and rebuild again." In Java, you can remove/change things as well. It's this thing called "deprecation." Perhaps you've heard of it. It's present in other languages as well. You tell people that something is changing/going away and then later on you change it or make it go away.
But then, more often than not, keeping the old code around doesn't hurt anything, it just doesn't change. So tell me, how many versions of the standard C library do you have around? And that's just one library! By comparison, the definitions for String and StringBuffer, two fundamental cl
- I don't need to go outside, my CRT tan'll do me just fine.
A much more better speled deskripshun of PHP5's OO language enhancements are is available from the horse's mouth
In what way is a standard library of 1500 classes, all documented in the same place, harder for developers to understand than 200 different libraries each implementing 10-15 classes?
Because the developer doeesn't have to learn 200 libraries, he'd only need to learn the subset he frequently uses. The java class library is very highly coupled. To use the networking part, you need to understand all the data structures part.
Of course 1500 classes are likely to contain more bugs than 15 classes. But do you have any evidence that those classes are likely to be buggier as a result of being standardised, than they would have been if they'd been written by dozens of different people and never tested together?
Bug occurance isn't linear with LOC, its exponential. The fact that with separate smaller libraries people would be focusing in on smaller chunks of code means each chunk would have fewer bugs. Bug occurance vs LOC relationships has a lot of research on it, they all say that small independantly written chunks get fewer bugs/LOC than large masses of code.
Do you have any evidence that it's better for the teams who write these 1500 classes NOT to talk to one another, standardise their design, and test their classes together, than it is for them to collaborate?
Can you keep track of every component of a million LOC project? I know I can't. As size goes up, complexity goes up, and bugs go up. This is basic software engineering, the research on this has all come to this conclusion since the 70s.
I'll put it this way: I am more likely to use advanced functionality in a language where that functionality is standard (and therefore present on everyone's computer) than I am in one where it is not.
No you aren't. If you're designing something, you use the functionality you need to get the features written. Using advanced functionality just for the sake of using it is a bad idea. Rule of thumb- if you use something because it seems like a clever trick, its going to be hard to debug, hard to maintain, and is more likely to have bugs than if you had done it the simple, "less advanced" way.
As for your swing exaple- horrid example as swing doesn't come close to working the same on Windows and Unix. ANd it doesn't address the point I made at all.
Which is great - until YOU find yourself wanting to use some old code that was written for a third-party library that was discontinued years ago. Small comfort that there's a new better library to frobnicate widgets, when you're faced with having to waste a month converting 200,000 lines of code to USE this new library, or installing Windows 98 on an old machine to use the only version of the old library you can find!
Instead you have cruft building up in the giant standard library, and people using that cruft in new applications, making it even more important to keep it. In the meantime, it becomes increasingly difficult to write Java implementatiions because the cruft needs to be carried forward, and youu lose the only benefit of the large class libraries- similarity of style- because the cruft can't be kept up to date without breaking compatibility
Huge standard libraries come with few, marginal at best pluses. In fact, the only one that comes to mind is writing all the classes in a similar style. The problem there is that the same style doesn't fit all types of classes, which is why Java's library is so poor.
I still have more fans than freaks. WTF is wrong with you people?
1. Classes are encapsulations of logic (variables and functions). What was once a shared library on its own can be distilled to one or a few classes. If each class had about five methods (functions) and two static properties (variables), the equivalent code body would be 7,500 functions and 3,000 global variables. Since classes/objects (and packages!) keep similar functionality compartmentalized, it's actually easier to keep a significant chunk in mind. Interfaces (method definitions without implementation) make it even easier.
And separate libraries keep it even more compartmentalized. My networking library requires no knowledge of my hash table or linked list. With one library like Java's, to know one part you need to know a large chunk of it. Part of it is the ridiculous depth of the hierarchy they use, part of it is due to the high degree of cross-linkng between sections. Its not a well designed library.
2. The smaller the library, the less it does. What's your point? At the end of the day, when your project (without objects or a complete standard library) is finally ready, you have linked to quite a few shared libraries, reinvented the wheel twice, and have very little standardized that would be recognized by the next person who comes to maintain the project.
THe point is that projects don't need the 1500 classes. THey'd be better off downloading libraries that just do what they need. Chances are those libraries will do a better job than the one size fits all standard library.
As for reinventing the wheel and no standardization- no, you search the web, find libraries, or go to a library repository to find one. No reinventing needed, and I've never had a problem to have standardization. Of course, maybe I'm just a better programmmer than you.
The more the standard library does, the less code (with bugs) you have to write and maintain.
No, when you write in a language without one you use libraries others have written still, either open source or for purchace.
3. Yes, better to compartmentalize items. Make separate components. Put them in... ummm... PACKAGES. Yeah, that's it. That way I can keep my general utilities and basic data structures separate from my I/O routines which are kept away from my database layer and my GUI and... java.util, java.io, java.sql, java.awt, etc. So what's the problem?
Because it doesn't work like that. Due to the standard library's attempt to keep consistent styles and interfaces, you get functionality thats shoved into implementations that don't work well. Meanwhile separate libraries get to pick the model that fits them best.
Huge projects like C's standard library and the hodgepodge of 3rd party libraries end up producing subpar code. Your point?
Umm, C's standard library has fewer functions than Java's does classes, its hardly huge. And using different libraries produce SUPERIOR code, bcause you pick the ibrary that best suits your code, not the one that came in the standard library because its there.
The best comparison is- Java's library is McDonalds. Its edible, you can get by on it, but it isn't very good.
Yes! Because Java hasn't changed in the last ten years at all. Pray tell, what innovations were you speaking of? The innovation of yet another person making yet another linked list or hashtable tailored exactly for their needs?
Wow, you're oblivious. There's been improvements on even simple sstuff like strings that has improved average performance in the past 5 years. COW is one that's come into use. Similar techniques have popped up in hash tables and lists. FOr large tables and lists, a library using modern techniques is at least 10% better than it was 5 years ago.
What about the fact that few C or C++ (or LISP or Perl...) coders bothered to make documentation at all let alone generate it from the source code itself until javadoc came along. Now it's all "we have that too!"
A
I still have more fans than freaks. WTF is wrong with you people?
Yes, Java developers will be pleased... because they'll stick with Java. PHP is destroying its niche by trying to go the Java-ish OO route.
A) Sun implemented too much functionality. (1, 2)
B) All of that functionality is built into one library instead of separate downloadable libraries. (3)
C) Every JVM is required to come with the library. (4, 5)
I respectfully but wholeheartedly disagree with you on all points.
A/1,2) I applaud Sun for raising the bar in delivering so much functionality. For the most part, the packages are well organized enough that the classes you don't use don't bother you. (For example, there have to be hundreds of classes in java.awt.*, but if you only write webapps or CLI apps, you don't even have to know they exist.) As for bugs, I haven't found the Java libraries to be more buggy than the vast majority of third-party libraries I've used. In fact, they are far less buggy because there are so many eyes on them (i.e. all the millions of Java developers out there).
B/3) Granted, not all of the packages are absolutely best of breed. (AWT/Swing come immediately to mind.) But most of them are quite well written, by fine minds like Josh Bloch and Doug Lea. I don't think there are many people in the world who could have written this. Overall I think the quality of code in most of the Java platform classes is at a level that almost any development organization would be proud of. And anyway, I don't see how code quality is related to a deployment decision like "should this be one JAR or seventeen?". What you're really saying is all this code shouldn't have been written by Sun, because you assume they don't have enough developers or that those developers are not focused enough on their tasks. (Right??)
C/4,5) The Java community has not proven to be short on innovation. There have in the past been plenty of third-party Java collection classes (Trove, Commons Collections, Commons Primitives, tclib, FastUtil, PCJ, Mango). I would like to see some more specific examples to back up your claim of "huge losses in productivity". (Although the general argument may not be totally without merit, because the .NET development community does in general seem to shun non-Microsoft libraries and technologies.)
As for your backward compatibility argument, I don't see how a widely used library should have any less of a responsibility to remain backward compatible with previous versions, than a language does.
And from just a common sense perspective, can you seriously mean that a modern language shouldn't have built-in types for such basic abstractions as string, list, hashtable? Do you really think this kind of crap is an improvement?
So you're saying that if a networking library requires a linked list or hashtable it should roll its own rather than reusing an existing, efficient implementation?
Also, your example seems contrived. Please show me where I need to access a linked list from within the classes in java.net. Will some List implementation be loaded by the classloader in your networking program? Probably, but not by the networking packages. If it's loaded, it's because the programmer needed a List.
Also, a JVM will likely have quite a few *.so files lurking in the background. Unless the classes that need the native support are loaded by the classloader, the loadlibrary call is never called for them. Class definitions aren't loaded into memory unless they're used.
You'd advocate splitting off the string handling functions of the standard C library from the other functions? Sure, it's a different header file, but it's the same *.so file. After all, all I need to do in my program is tokenize a string and spit it back to the console. Why should I have to map a 1.2MB library just for that? (By the way, I was being facetious.)
Or maybe you enjoy dealing with twenty different ways of threading. And if you point to your one favorite threading library, do you use that same library on UNIX, Mac, et al.? Or are you the type that states that threading has no place in software -- a fork is the only true way kinda guy. What about database access? ODBC or do you code each and every database separately? (Then again, with ODBC, you have to make enough special cases that indeed you do code each and every database somewhat differently.)
My point is these are solved problems. And for that matter, there is nothing stopping a person from writing their own linked list or hashtable implementation. Nothing. Why doesn't anyone? Because over and over what has been seen is that the standard ones are either (a) better or (b) good enough that the extra effort isn't worth it. I know that (b) probably upsets you, but if my project isn't about hashtables and the difference between the two implementations would have a negligible impact on the overall project, it's not worth doing. Reinventing the wheel in this case is what KILLS innovation.
Enough hyperbole. Please show me where the JSL is deficient. Let's debate specifics.
It's funny you should mention that. I'm assuming you are talking about C++'s std::string and using type traits and template parameters to make appropriate forms of strings for the task at hand. (See, I read Modern C++ Design and Dr. Dobbs too.) First,
- I don't need to go outside, my CRT tan'll do me just fine.
Your argument seems to revolve around the fact that the J2SE library is too big and poorly designed. Do you actually have proof to back this up, or is just heresay?
You also make the point that it is good to be able to pick and choose which library to use, slating Java because it does not allow this. Since when did Java not allow you to use whatever library you want? If you want to write your own HashMap, LinkedList or implementation of the networking libraries, or use someone else's, go right ahead. Whether or not it is a good idea to continually reinvent the wheel is another matter. Standardisation and standard libraries bring with them many benefits.
J2EE consists of a bunch of specs and reference implementations, encouraging different implementations of a single specification to be developed for different needs.
I'll continue to not use them and end up with easier to write and better performing applications than yours.
That's fine, but don't forget that PHP is merely a web scripting language.
In this world nothing is certain but death, taxes and flawed car analogies.