473,511 Members | 15,178 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Concatenating object methods

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
3 1474
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
..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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2038
by: dont bother | last post by:
Hey, I have these attributes: index which is a numerical value value vector which is a numerical float value and I want to concatenate like this:
14
1732
by: foodic | last post by:
i am fresher to C++ programming, and I just want to learn Concatenating Calls, I have written a program, class SetMe { public: void setX(int x) {_x = x;} void setY(int y) {_y = y;} void...
4
2319
by: Juan | last post by:
Does any one know if there are reported bugs when concatenating strings? When debugging each variable has the correct value but when I try to concatenate them some values are missing (I can´t see...
4
11775
by: Elena | last post by:
Hi, I am filling in a combobox. I would like to concatenate two fields into the data combo box and display "last name, first name" I tried to displaymember = "employee_last_name" & ", " &...
4
1853
by: Luke Matuszewski | last post by:
Here are some questions that i am interested about and wanted to here an explanation/discussion: 1. (general) Is the objectness in JavaScript was supported from the very first version of it (in...
11
3797
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
12
5505
by: Andrew Poulos | last post by:
With the following code I can't understand why this.num keeps incrementing each time I create a new instance of Foo. For each instance I'm expecting this.num to alert as 1 but keeps incrementing. ...
7
2761
by: Mary | last post by:
I have a student who has a hyphenated first name. If I concatenate the name like this: StudentName:( & ", " & ), it works as expected. If, however, I try to get the first name first by...
10
3463
by: Neil | last post by:
Using the MS Rich Textbox Control 6.0 in Access 2000, I need to concatenate several RTB controls into a single RTF file. Sometimes two strings will be side-by-side; other times they need to be...
0
7237
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7349
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7074
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5659
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5063
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4734
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3210
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
445
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.