Jump to content

[SOLVED] Setting a static variable in a new class instance.


ridiculous

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";

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.