Avatar of tonelm54
tonelm54

asked on 

OOP Question

Im fed up of writing a lot of complex HTML code, so thought about writing it in a nice easy object, but what I want to do is build up a statment via multiple calls. For example:-

$myObject->createDiv("divID")->createTextBox("txt1")->createTextBox("txt2")->createTextBox("txt3");

Open in new window


Ive got my code nicely working with:-
$myObject->createDiv("divID");

Open in new window


But I have no idea how I can create my object to accept multiple createTextBox fields. I know its possible as Ive seen it done in PHPCharts library, just no idea how to implement it myself.

I know my examples look easy, but what I want to do inside 'CreateTextBox' is set height, width, ID, Classes, Data objects etc, hence why it becomes complicated, but want to try and make my life easier, and hence the simple example to start with and then I can build out.

Any ideas?
PHP

Avatar of undefined
Last Comment
tonelm54
SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of hielo
hielo
Flag of Wallis and Futuna image

>> Ive got my code nicely working with:
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.
Avatar of tonelm54
tonelm54

ASKER

Ok, so quickly reading things, what Ive got is:-
<?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");
?>

Open in new window


But I get
Exception: Call to a member function createTextBox() on string

I know the HTML isnt right, just testing at the moment.
ASKER CERTIFIED SOLUTION
Avatar of tonelm54
tonelm54

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Unless you can think of a really important reason to mark a method or property "private" don't do that.  It makes that part of the class into something that cannot be extended.  Instead start with everything public and then make a list of reasons why any of the visibility characteristics should change.  You will probably come to the conclusion that protected properties and public methods make a lot of sense.

Best of luck with the project! ~Ray
Avatar of tonelm54
tonelm54

ASKER

:-) Got it working thanks to your advise again
PHP
PHP

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.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo