We help IT Professionals succeed at work.

OOP Question

tonelm54
tonelm54 asked
on
36 Views
Last Modified: 2017-04-26
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?
Comment
Watch Question

Most Valuable Expert 2011
Author of the Year 2014
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Expert of the Year 2008
Top Expert 2008

Commented:
>> 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.

Author

Commented:
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.
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Most Valuable Expert 2011
Author of the Year 2014

Commented:
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

Author

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

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.