PHP DOMNode -> setAttribute() max-width confusion

While adding some custom code into Mahara, I came across what seems like a very odd PHP bug today, so odd that I put it down to user error, but just can’t seem to find what the user error is! Imagine the code below with some embed code from youtube or picasa as the value of $embedCode.
$embedCode = "<embed .................></embed>";
$dom = new DOMDocument();
$dom->loadHTML($
embedCode);
$allElements = $dom->getElementsByTagName('*');
$firstElement = $allElements->item(2);
$
firstElement->setAttribute("style", "max-width:100%");
echo $dom->saveHTML();

The line that plays up is the one including setAttribute(“style”, “max-width:100%”).

It works on pretty much all other possible css values, but doesn’t seem to like max-width nor min-width. I can set the style to hold color or background values, or text-align, but not max-width! What gives? Why does PHP seem to have CSS max-width?

EDIT: I have since uncovered the cause of this. My text string was being passed through a Mahara function clean_html() which strips out various things, including max-width style definitions. The solution is to clean_html() before I apply my max-width and hey presto, everything works as expected.

Book Review: Object-Oriented PHP

A few months ago a package came through the letter box, bringing excitement into my life. I received a copy of Object-oriented PHP to review. This is the first time I’ve received a book to review, and my excitement was such that I was at once looking forward to writing something glowing with praise. My immediate excitement has died down over the months since I received it, and having now read it I feel able to subjectively write a review. Any praise which glows does so genuinely and not out of any any initial ebullience.

Lifting from the back cover of the book “Peter Lavin runs a web development firm […] has been published in a number of magazines [… and] is a contributor to PHP Hacks (O’Reilly)”

The books is aimed at PHP developers without much object-oriented experience, and at object-oriented programmers wanting to learn PHP.

I come to the book having taught myself PHP a little over 3 years, and having studied object-oriented programming at University about four years ago. I regard myself as still learning PHP, picking up the bits I need to know as and when necessary, and I haven’t done all that much object-oriented programming in the past four years. I’m quite sure this makes me almost the ideal audience for this book.

My initial perception of the book is that it is well written, and immediately engages the reader with practical and clearly explained examples. Code is provided that will give the author an immediate ‘win’ in adding certain common functionality to their site.

Chapter one starts with a quick overview of object-oriented programming, giving the arguments for and against, and ultimately coming down on the side that every additional tool that might make the programmer’s job easier is well worth learning, and that the author is free to decide for themselves what to use.

Chapter two gives a quick introduction to the concepts of object-oriented programming, touching ever so briefly on reuse and forms of inheritance. Being quite comfortable with the concepts of OOP I found this chapter (all four pages of it) a nice refresher. Anyone new to OOP might be left feeling as if they are being dropped in the deep end, wanting more information to fully grasp OOP concepts before continuing. However, later chapters do not assume too much from the reader, and when OOP concepts are raised, they are done so with further explainations. An insecure reader should press on, confident that they are not expected to grasp the intricacies of OOP from this chapter alone.

Chapter three takes a look at the object-oriented features of PHP5 as compared with PHP4. Anyone confident with PHP4 should find this chapter useful. In contrast with chapter two, old-hands at PHP should fly over this chapter comfortably while the confident object-oriented programmer new to PHP may find the comparisons between the versions of PHP non-relevant.

With chapters four to ten the overviews and principles are behind us, and we start meeting solid code examples of how to get things done. Example code is immediately useful, and from chapter to chapter the code builds up in functionality. In addition to leading the reader slowly through the increasingly effective example code, I felt the benefits of the object-oriented approach are subtly demonstrated. As a fan of OOP I felt as if this was exactly what someone new to OOP needs to see: the increasing complexity of the code pushed into classes, while remaining easy to call upon and use.

Chapter eleven introduces more advanced OOP concepts and techniques which from my own perpective are easily understood. Someone new to OOP might find the easy and gentle approach to the theory a little too hands-off which might have a negative impact upon thorough understanding.

Chapters twelve to sixteen give a cursory introduction to things like simpleXML, RSS, the Google API, AJAX, SOAP, magic methods, reflection, extending SQLite, and PHP Data Objects. Just enough detail is given to whet the appetite, but anyone interested in these areas would be well advised to get a more advanced PHP book.

For those less concerned with understanding OOP in the abstract, and just interested in getting on with using OO-PHP and getting stuff done, I think this book hits the right tone. This book certainly lives up to the subtitle of: Concepts, techniques, and code. If you are part of the target audience for this book, I think you will find it of great value, and I can fully recommend it.

This is the first book I’ve ever read on PHP, so I can’t compare it to other books. However, while the book is easy to read, if I was new to PHP and didn’t have a grasp on OOP, I’m not sure this book would have worked well for me. If the most damning thing I can say about the book is that it doesn’t work well for those it isn’t targetted to, then it is clear that this book gets a double thumbs-up from me.


Before I could publish this post (while I was working on the draft), it seems someone else has reviewed this book on osnews.com, if you want to see what someone else thought it is well worth a look.