开发者

What should I put into classes and what stuff I shouldnt?

I am learning about classes right now in PHP and their examples are like..

class table { //makes a table
    private $tag ;
    function Begin($border=0, $align="center", $width='100%', $cellpadding=2, 
        $cellspacing=2, $class='', $id='', $bgcolor='', $style='') {
        $this->tag = '<table ' ;
        if ($align)              $this->tag .= 'align="' . $align . '" ' ;
        if ($width)              $this->tag .= 'width="' . $width . '" ' ;
        if ($border > 0)         $this->tag .= 'border="' . $border . '" ' ;
        if ($cellpadding > 0)     $this->tag .= 'cellpadding="' . $cellpadding . '" ' ;
        if ($cellspacing > 0)     $this->tag .= 'cellspacing="' . $cellspacing . '" ' ;
        if ($class)              $this->tag .= 'class="' . $class . '" ' ;
        if ($id)                  $this->tag .= 'id="' . $id . '" ' ;
        if ($bgcolor)              $this->tag .= 'bgcolor="' . $bgcolor . '" ' ;
        if ($style)              $this->tag .= 'style="' . $style . '" ' ;
        $this->tag .= ">" ;
        return $this->tag ;
    }

Then you just instantiate it and make a table by

$table =new table;
$table->$table($border=2, $align='center', etc);

Should I be coding like this where html, css are in classes? i feel making tables and forms this way is more confusing then actually just typing . Should I only put like validation, getting data from db开发者_StackOverflow社区, and the logic stuff in classes?

What should I use classes for and not?


You could do that... personally, I wouldn't.

Use objects for domain objects. As an example, imagine the source code for Facebook. I'm pretty sure that somewhere in the Facebook source code you'd find the following:

class Friend
{
    // ...
}

... equally, were SO written in PHP you'd find something like this:

class Question
{
    var $originalPoster;
    var $answers = array();
    // ...

    function close()
    {
        // ...
    }
    // ...
}

Use objects to encapsulate complex ideas and concepts. On SO there's a lot of information associated with a question, but you can encapsulate that information in an object.


Maybe this would clean it up. Better to expand the class to encapsulate the entire table, and use overloading so you don't even have to keep track of anything. All you really need to be concerned about is HTML validation, using valid HTML table attributes. Really, as said earlier, objects just make it easier to conceptualize what you want to do and how you want to organize your ideas.

With the following solution, you don't need to be concerned with the object construction and default values so much as with just html validation. After constructing it with an array of html table element parameters (if you even want to), you can just overload the object with parameters after that too. You can also easily add rows. Completely untested though and overly simplified...

Class My_table {
   private $table_def = array();
   public $rows = array();

   function __construct($attributes_array) {
      if (is_array($attributes_array)) {
         foreach ($attributes_array as $key => $val) {
            $this->__set($key, $val);
            }
         }
      }

   function __set($attribute, $value) {
      $this->table_def[$attribute] = $value;
      }

   function draw() {
      // write table opening tag
      echo "<table";
      foreach($table_def as $key => $val) {
         echo " $key='$val'";
         }
      echo ">";
      // write all rows out
      foreach($rows as $current_row) {
          echo "<tr>";
          foreach($current_row as $field_name => $field) {
             echo "<td>$field</td>";
             }
          echo "</tr>";
          )
      // finish table
      echo "</table>";
      }
}

$table = new My_table(array(
   "style" => "boarder: solid thin gray;",
   "width" => "100px"
   ));
$table->height = "200px";
$table->rows[] = array("name" => "someone", "email" => "a@a.com")
$table->draw();


Objects can represent anything you want them to, but it primarily depends on what kind of application you're making. Taking your example of a class for an HTML <table>, if your PHP software doesn't really care much about HTML, there's not much sense creating a PHP class for an HTML element.

Think about your application. What objects are involved with it? For instance, given a single-author blog, there can be posts, comments, categories and tags. The corresponding classes that you'd want to have would, naturally, be Post, Comment, Category and Tag.

Hopefully that made sense (it's a little late here...).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜