Hi, On Wednesday 20 December 2006 13:37, Roman Neuhauser wrote:
> # [EMAIL PROTECTED] / 2006-12-19 19:05:23 +0100: >> I'm writing a PHP program and I've got a number of concerns. The program >> is about 20 KLOC at the moment, but will probably grow quite a lot. >> >> I'm using OOP throughout, and I don't really have a lot of problems with >> what PHP4 can do with OOP. PHP5 does have a nice feature or two, >> eventhough none seems essential to my particular style of programming. I >> don't mind using what PHP5 offers where it makes sense, but where's that? >> Exceptions and new OOP features? >> >> Exceptions: I just don't see the big advantage. I've got errors and >> messages bubbling up through object layers as it is, and exchanging that >> with a wholly new structure seems more trouble that it's worth. I've read >> several books on how cool PHP5 is, but the arguments for using exceptions >> seem to boil down to "Java has them". Nowhere have I seen good examples >> that really show how well exceptions solve real problems, all examples >> seem to show are that 5 lines of try/catch are somehow sexier than 5 >> lines of if/else. > > One of the differences is that the if/else lines need to be in all layers > between where the error might happen and where it will be ultimately > handled. With exceptions, the try/catch can be detached (as long as it's > the right thing to do, of course). > You're right, that is an advantage. I'll need to rewrite much of my current code to make use of it, so that's not a high priority. More importantly I have to start thinking in this new way to use exceptions properly. >> What about performance? > > Did you measure the performance impact of all those if/else's? > > Exceptions are a special channel for errors, so your question is kind of > like "is stderr any good? what about performance?" > If two different ways of doing the same thing seem very similar, performance may help me choose. It's true that I don't know anything about the speed of all my nested if/elses, but that's not necessarily relevant if others can tell me that exceptions are always hopelessly slow. >> New OOP features: I can go through my code and mark all my methods as >> public or private or whatever. No problem. But why would I? It will make >> classes easier to understand when I look at them, but that's just >> convenience. What are the performance benefits? I've not found a single >> mention of that anywhere. What do abstractions and interfaces actually >> do, aside from structuring my code better? > > What do PHP4 classes actually do for you, aside from structuring your > code? What about performance? Wouldn't you be better off if you wrote all > of your program into a single file, all in global scope, using only > builtin functions > and primitive data types? It would surely be faster, and you'd only lose > the convenience, no? > I get your point, everything above assembler on the metal can be considered "merely convenience". In this case, however, there's a detail to my question: I'm basically asking whether adding public/private to methods will enhance performance. I could be -- I don't know about these things -- that the PHP processor could use such keywords to optimize the execution. >> What major compelling reasons do I have to start using exceptions and >> OOP-5? > > All the things you mentioned, and then some. > > Someone else mentioned that PHP 5 is much less inclined to copy objects. > You still don't get the convenience of a private copy constructor, but > hey. > > Another thing is destructors, so you're able to mimic C++'s powerful > > // unlocked > { > mylock_t lock; > // locked > } > // unlocked > > (not so powerful in PHP without anonymous scopes). > > For example, a unit-testing library for PHP 5 called Testilence provides > two utility classes, a temporary dir and a temporary file (see mkdtemp(3), > mkstemp(3)). Both classes remove the underlying filesystem objects in > their destructors, so you can conveniently skip doing the cleanup > yourself: > > function test_O_EXCL_ThrowsOnExistingPath() > { > $file = $this->mkstemp(); > $this->willThrow('RuntimeException'); > new SplFileObject($file->path(), 'x+'); > } > > Also, notice how the code can omit checking for errors in mkstemp(). > The return value is guarranteed to be the right thing, since any errors > would be signalled by throwing an exception, and that is handled By the > caller of this method. > That looks interesting, thanks. > How about iterators? You can have objects that look like arrays yet they > take much less memory: > > $rs = $db->query($select); # query the db > foreach ($rs as $row) { # fetch the row > whatever($row); > } > "takes much less memory" is exactly the kind of advice I'm looking for. I've found very few usable guides to such optimizations. Could you possibly give me a specific example of an array and a similar object with this great difference in memory consumptions? Thank you for your answer, Niels -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php