Luyenkim.Net!

...where your idea grows!

 
  • Decrease font size
  • Default font size
  • Increase font size
HTML Output Class Redefined PDF Print E-mail
(0 votes)
Thứ sáu, 23 Tháng một 2007
Article Index
HTML Output Class Redefined
Continued

Remember how we had to pass those same common variables to every output function we create? There must be a better way to keep track. Consider this. If we add variables to our output class then they will always be available when we need them. So let's create a new output class with the following goals in mind:

  1. Store our common variables
  2. Create some paths we'll always need
  3. Simply our coding by reusing code
  4. Never have to debug a missing common variable again!

The Logic

Here's our new logic function. There are a couple of things different here:

  1. We create an ouptput object using our new class and pass all our common variables to it.
  2. We only pass our output data to the function. This helps keep our system and business data separate
function helloLogic( $id, $option, $task, $Itemid ) {
global $my, $mosConfig_live_site;
// do some processing
$id = (int)$id;
$name = $this->my->name;
// output our data
$output = new HTML_myOutput( $task, $option, $Itemid, $my, $mosConfig_live_site );
$output->hello($id);
}
The Class

There's a lot more going on here than in our old class. We have some object properties (variables). And we have a constructor to instantiate our new class. It looks like a lot but believe me it'll save you alot of headaches in the long run. Plus, you can reuse your new output class and never have to worry about those pesky common variables again.

Take a look:

class HTML_myOutput {	
var $task = '';
var $option = '';
var $Itemid = '';
var $my = '';
var $liveSite = '';
var $jsPath = '';
var $imagePath = '';
var $returnPath = '';
function HTML_myOutput($task, $option, $Itemid, $my, $liveSite) {
$this->task = $task;
$this->option = $option;
$this->Itemid = $Itemid;
$this->my = $my;
$this->liveSite = $liveSite;
$this->jsPath = $this->liveSite."/components/".$this->option."/javascript";
$this->imagePath = $this->liveSite."/components/".$this->option."/images";
$this->returnPath = $this->liveSite."/index.php&option=".$this->option."&Itemid="$this->Itemid;
}


Let's see how we did on our goals:

  1. Store our common variables
    Yep, all those common variables are right there in the class properties were we can get to them.
     
  2. Create some paths we'll always need
    We used our class properties (variables) to setup some reusable paths we might need in all our output.
     
  3. Simply our coding by reusing code
    With some simple cut-n-paste keystrokes, we can reuse our new class as many times as we need.
    This will keep things nice and consistant.
     
  4. Never have to debug a missing common variable again!
    As long as we always create an instance of our output class the same way using cut-n-paste, we'll never have a problem.
The Output

Pretty much the same. Except we only pass our business data to the function. Everthing else was passed to the output object for consistency.

We used our new object properties to create some variables we need in the output. The purist might say we should'nt put code in our output function. But doing so makes things a little cleaner when we're writing the html.

<?php function hello( $id ) {
$name = $my->name;
$return = $this->returnPath;
?>
<p>Hello, <?php echo $name<br /> The value for $id is <?php echo $id; ?></p>
<p> </p>
<p><a href="<?php echo $return; ?>">Return to My Component</a></p><?php
}
?>
Conclusion

We created some overhead when we created our new class. But it should make the code more consistant and reliable. Plus, all our common variables will be available in any function we create within the class.

There are couple of things to remember about our new class:

  1. Since we need our class properties in the output, we must always instantiate our class object.
  2. We can no longer use the scope resolution operator (::) to call an output function like we used to - HTML_myOutput::hello($id).

I hope the above method will save you some time and headaches when you're writing your next component. I know it worked very well for this me. When you're writing hundreds or thousands of lines of code, every advantage you can find makes life easier for you.

Source: http://joomlabr.net




Related news items:
Newer news items:
Older news items:

 

Please install Flash and turn on Javascript.

Đăng nhập






Lost Password?
No account yet? Register

Liên kết website

Luyenkim.net
Luyenkim.net
Sửa ĐTDD
vinaora.com
http://www.clevermind.com.vn/
thietbidien.vn

Thống kê

We have 19 guests online