asked on
$myObject->createDiv("divID")->createTextBox("txt1")->createTextBox("txt2")->createTextBox("txt3");
$myObject->createDiv("divID");
ASKER
<?php
class tstHTML {
private $strHTML;
public function createDiv (string $id) {
return "divID=$id-" . $this->strHTML . "-div";
}
public function createTextBox(string $id) {
$this->strHTML .= "txtID=$id,";
return $this;
}
}
$tt = new tstHTML();
echo $tt->createDiv("div1")->createTextBox("txt1")->createTextBox("txt2");
?>
Exception: Call to a member function createTextBox() on string
ASKER
PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.
TRUSTED BY
It would help if you post what you have. Are you buffering or are you echoing right away?
For the chaining to work, you need to return $this on all the chainable methods.
>> but what I want to do inside 'CreateTextBox' is set height, width, ID, Classes, Data objects etc
pass an associative array instead of simple string. Then within the respective method you need to iterate over the array properties to decide what you want to do.