Jump to content

Recommended Posts

Before I show you how to do it, did you even try to read the manual on classes?

 

class Test {
    public $variable;
    public function __construct($var) {
           $this->variable = $var;
    }
    public function show() {
           echo $this->variable;
    }
}
$test = new Test("I'm a variable!");
$test->show();

Well, I have a database class like so:

 


class Default_Table
{
 var $tablename;         // table name
 var $dbname;            // database name
 var $rows_per_page;     // used in pagination
 var $pageno;            // current page number
 var $lastpage;          // highest page number
 var $fieldlist;         // list of fields in this table
 var $data_array;        // data from the database
 var $errors;            // array of error messages

 

The way the class is written, you access the methods inside it by extending the class and sending the variables through a function. Like this:

 

class Sample extends Default_Table
{
   // additional class variables go here
   function Sample ()
   {
       $this->tablename       = 'sample';
       $this->dbname          = 'foobar';
       $this->rows_per_page   = 15;
       $this->fieldlist       = array('column1', 'column2', 'column3', ...);
       $this->fieldlist['column1'] = array('pkey' => 'y');
       et cetera ...

   } // end class constructor

} // end class


$s = new Samle();
$s->Sample();

 

Instead of extending Default_Table, I figure I'll just go:

 

new Default_Table($tablename=" ", $db_name=" ", $rows_per_page=" "...)

 

To me, that's simpler.

where someone uses new ClassName('internal variable value'); ?

 

btw the below code you wrote is wrong, you executed twice the class constructor

 

class Sample extends Default_Table
{
   // additional class variables go here
   function Sample ()
   {
       $this->tablename       = 'sample';
       $this->dbname          = 'foobar';
       $this->rows_per_page   = 15;
       $this->fieldlist       = array('column1', 'column2', 'column3', ...);
       $this->fieldlist['column1'] = array('pkey' => 'y');
       echo 'constructor called';		
   } // end class constructor

} // end class


$s = new Sample(); // executes method Sample()
$s->Sample(); // executes method Sample() again

 

btw if you want that Default_Table defines your default behaviour you should declare it abstract

 

abstract class Default_Table
{
      protected $_cols;
      protected $_primary;
}

class TableName extends Default_Table
{}

 

for more on this subject, you should check the Table Data Gateway

http://martinfowler.com/eaaCatalog/tableDataGateway.html

What?  It's called a constructor, and he provided the correct documentation.    Also, why are you using PHP4 OOP syntax rather than PHP5?  And can I see the rest of the class because I don't understand your original problem.

Its from a PHP tutorial. The DB class was done in PHP 4. I didn't think there were any significant changes that PHP 5 would make...I'm going to try to merge this class with PDO.

 

http://www.tonymarston.net/php-mysql/databaseobjects.html

I like PDO. http://www.php.net/manual/en/pdostatement.execute.php

 

I want to use prepared statements, but I want to combine them with a method that allows me to pack the query string like so:

 

$query = "SELECT $select_str FROM $from_str $where_str $group_str $having_str $sort_str $limit_str";

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.