print(object)

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
lapith
Forum Newbie
Posts: 11
Joined: Fri Jul 27, 2007 4:37 pm

print(object)

Post by lapith »

A co-worker of mine (a more experienced PHP'er, as I am just learning it) had suggested that I use a class to generate the dynamic parts of the page that I need. One such area is the navigation bar, so I will refer to that in my question.

The way he described it is that the class (or object) is a "string" and in the HTML all I would have to do is use the print() directive to generate the HTML needed in that spot. (so I guess it would look something like <div class="nav"> print nav_generator.php </div>)

Coming from more of an application programming background I was thinking that this was simply using a _to_string sort of thing, but he seemed to disagree with me on that. When asked for an example or a better explanation I was simply given a link to the OOP portion of the PHP manual, and I was unable to find an example of this technique within the documentation.

If anyone has any idea of what I am talking about I would love to hear back from you. Currently I am working on just using server side includes and echo-ing out HTML, the class/print() design seems much more elegant.

(Also I believe that he mentioned that drupal uses this technique, if that sheds any light on the situation.

Again. any help would be appreciated.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

I've never taken the time to read Drupal's jumble of code, so I couldn't give you anything in that respect.

I think what he means is that you can create a print() method within the class that would output the final product of the class. Of course, contrary to what you've been told, using __toString() would work just as well, though it's good practice to have __toString() call the print() function rather than having __toString() be the only method, just in case you'd need to use the code in a PHP 4 environment.

Drupal, as far as I know, is function-based rather than object-oriented. It is procedural, but a much more advanced form of procedural code than most OO programmers associate procedural programming with. It is almost like OOP, but (according to them) faster and just as organized. It also appears to be plugin friendly, but OO-based projects can be just as friendly.

It wouldn't look like the way you've written, but moreso like:

Code: Select all

<div class="nav">
<?php
$pNav = new CNav();
$pNav->run();
echo $pNav->print();
?>
</div>
Or, if you were to use the __toString() function, which would call the print() function, and set up your class so that if the contents were empty, it'd call the run() function inside of the print() function before generating the output, then it could be this:

Code: Select all

<div class="nav">
<?php
echo new CNav;
?>
</div>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: print(object)

Post by Christopher »

I am a little baffled by:
lapith wrote:The way he described it is that the class (or object) is a "string" and in the HTML all I would have to do is use the print() directive to generate the HTML needed in that spot.
And:
lapith wrote:Currently I am working on just using server side includes and echo-ing out HTML, the class/print() design seems much more elegant.
echo() and print() are aliases of the same function so I am not sure the true difference between the two things you describe. Perhaps the __toString() as mentioned is what you are looking for, but that just allows you to use an object in a string context. And that may not be clearer codewise that using a print() or render() method.
(#10850)
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Note that print is a keyword, so PHP will complain if you attempt to define a method called "print".
Post Reply