Reply to Re: Concatenating object methods

Your name:

Reply:


Posted by Tom on 01/31/07 19:50

On Jan 31, 11:45 am, "crater" <iccar...@gotadsl.co.uk> wrote:
> I'm using PHP 5.2.
>
> I've created classes to embody HTML elements for a particular project
> I'm currently working on.
>
> All setter methods in the classes return $this so that I can string
> the
> methods together in an attempt to reduce "page bloat".
>
> [code]
> // create a text link
> $link1 = new htmlElement("a");
> $link1->addAttribute("href", "http://homepages.nildram.co.uk/
> ~iccarter")
> ->addAttribute("target","_blank")->addAttribute("name",
> "htmlElement1");
> echo $link1->addContent("My personal web site")."<br><br>\n";
> [/code]
>
> My anchor element is duly output exactly as expected.
>
> A question naturally arises from this. Is there a way to do the
> following?...
>
> [code]
> $link1 = new htmlElement("a")->setAttribute("name", "value");
>
> or
>
> ($link1 = new htmlElement("a"))->setAttribute("name", "value");
> [/code]
>
> It's not an issue. I'm just curious.

This probably won't work but you could try returning $this in your
constructor:

[ in your class definition: ]
function __construct( $element ) {
// some code
return $this ;
}

This isn't the best programming though... a better way to get the
results you need would be to make constructor parameters optional, so
you can pass what you need to when you're creating a new instance of
this class, e.g.:

function __construct( $element, $name = "", $value = "" ) {
if ( $name && $value ) {
$this->setAttribute($name, $value) ;
}
}

I realize stringing together methods might be good now for a clean
looking page, but if you need to modify your code down the line (or
worse another programmer needs to take over the project), this will
probably cause more problems than it solves. Just a suggestion!

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация