By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,825 Members | 1,364 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,825 IT Pros & Developers. It's quick & easy.

Concatenating object methods

P: n/a
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".

Expand|Select|Wrap|Line Numbers
  1. // create a text link
  2. $link1 = new htmlElement("a");
  3. $link1->addAttribute("href", "http://homepages.nildram.co.uk/
  4. ~iccarter")
  5. ->addAttribute("target","_blank")->addAttribute("name",
  6. "htmlElement1");
  7. echo $link1->addContent("My personal web site")."<br><br>\n";
  8.  
My anchor element is duly output exactly as expected.

A question naturally arises from this. Is there a way to do the
following?...

Expand|Select|Wrap|Line Numbers
  1. $link1 = new htmlElement("a")->setAttribute("name", "value");
  2.  
  3. or
  4.  
  5. ($link1 = new htmlElement("a"))->setAttribute("name", "value");
  6.  
It's not an issue. I'm just curious.

Jan 31 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Tom
On Jan 31, 11:45 am, "crater" <iccar...@gotadsl.co.ukwrote:
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".

Expand|Select|Wrap|Line Numbers
  1. // create a text link
  2. $link1 = new htmlElement("a");
  3. $link1->addAttribute("href", "http://homepages.nildram.co.uk/
  4. ~iccarter")
  5.       ->addAttribute("target","_blank")->addAttribute("name",
  6. "htmlElement1");
  7. echo $link1->addContent("My personal web site")."<br><br>\n";
  8.  

My anchor element is duly output exactly as expected.

A question naturally arises from this. Is there a way to do the
following?...

Expand|Select|Wrap|Line Numbers
  1. $link1 = new htmlElement("a")->setAttribute("name", "value");
  2.                   or
  3. ($link1 = new htmlElement("a"))->setAttribute("name", "value");
  4.  

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!

Jan 31 '07 #2

P: n/a
..oO(crater)
>A question naturally arises from this. Is there a way to do the
following?...

Expand|Select|Wrap|Line Numbers
  1. $link1 = new htmlElement("a")->setAttribute("name", "value");
  2.                  or
  3. ($link1 = new htmlElement("a"))->setAttribute("name", "value");

It's not an issue. I'm just curious.
Nope. PHP's syntax doesn't allow that. You could write a function that
creates and returns the new object, something like

$link = createHtmlElement('a')->setAttribute(...);

Micha
Jan 31 '07 #3

P: n/a
On Jan 31, 8:45 am, "crater" <iccar...@gotadsl.co.ukwrote:
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".

Expand|Select|Wrap|Line Numbers
  1. // create a text link
  2. $link1 = new htmlElement("a");
  3. $link1->addAttribute("href", "http://homepages.nildram.co.uk/
  4. ~iccarter")
  5.       ->addAttribute("target","_blank")->addAttribute("name",
  6. "htmlElement1");
  7. echo $link1->addContent("My personal web site")."<br><br>\n";
  8.  

My anchor element is duly output exactly as expected.

A question naturally arises from this. Is there a way to do the
following?...

Expand|Select|Wrap|Line Numbers
  1. $link1 = new htmlElement("a")->setAttribute("name", "value");
  2.                   or
  3. ($link1 = new htmlElement("a"))->setAttribute("name", "value");
  4.  

It's not an issue. I'm just curious.
Why dont you just do this:

$link1 = new htmlElement("a");
$link1->setAttribute("name", "value");

If nothing else your idea will lead to confusion when you come back to
it later in life.

The rule i have always tried to follow (except in perl) is ...
One statement
One line

Jan 31 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.