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();

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.