Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Writing Constructor for php class with database function

Status
Not open for further replies.

mk2lee1

MIS
Joined
Jul 8, 2003
Messages
51
Location
US
Hi, I would like to make a class called "Game." basically I have some game table in the database with game attributes like score, team1, team2, datetime, etc.

I would like to turn this into the class but I have some difficulties writing the code.

1) how can I write the constructor?
2) Can there be 2 constructor? (one with attributes and other without attributes)
3)If I want to get the data from the game databse what my constructor should be?
4) and if I want to add some new game in to the table.. how should I use class to do this?

can anyone tell me about this?
 
I guess you read
1. the constructor has the same name of the class, example:
Code:
class menuitem {
  var $caption;
  var $url;
  function menuitem($uri, $cap){
       $this->url=$uri;
       $this->caption=$cap;
  }
  function print(){
      echo "<a href='$this->url'>$this->caption</a>'\n";
  }
}

item = new menuitem("[URL unfurl="true"]http://site.com","To[/URL] my site");
2. I'm not sure, never tryed, but there is an example in php.net:

Code:
/**
  * Example Class for getting around the problem with
  * overloaded functions constructors.
  *
  * This exapmple are asuming that you want to overload
  * the constructor Object() with different arguments. 
  */
class Object {

  /**
   * The real constuctor for this class.
   *
   * NOTE: Parlameter may not have the value null
   *      unless you changes the default value in
   *      the constructor.
   */
  function Object($param1 = null, $param2 = null) {
   $numargs = func_num_args();
   $arg_list = func_get_args();
   $args = "";
   for ($i = 0; $i < $numargs && $arg_list[$i] != null; $i++) {
     if ($i != 0) {
       $args .= ", ";
     }

     $args .= "\$param" . ($i + 1);
   }

   // Call constructor function
   eval("\$this->constructor" . $i . "(" . $args . ");");
  }

  /**
   * Functiorn that will be called if constructor is called
   * with no parlameter as agument.
   */
  function constructor0() {
   echo("Constructor 1: No parlameter." . "\n");
  }

  /**
   * Functiorn that will be called if constructor is called
   * with one parlameter as agument.
   */
  function constructor1($param) {
   echo("Constructor 2: \$param=" . $param . "\n");
  }

  /**
   * Functiorn that will be called if constructor is called
   * with two parlameter as agument.
   */
  function constructor2($param1, $param2) {
   echo("Constructor 3: \$param1=" . $param1 . "\n");
   echo("Constructor 3: \$param2=" . $param2 . "\n");
  }
}

new Object();
new Object("A String value");
new Object("Another String...", 1);

// Output:
Constructor 1: No parlameter.

Constructor 2: $param=A String value

Constructor 3: $param1=Another String...
Constructor 3: $param2=1

3. use mysql functions inside the constructor:
Code:
class menuitem {
  var $caption;
  var $url;
  function menuitem($uri, $cap){
          ...code for read DB...        
  }

4. depend what you want.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top