ridiculous Posted August 11, 2008 Share Posted August 11, 2008 class Test { public $variable; function Echo_It ( ) { echo $this->variable; } new Class($variable="I want this variable to be set in the class"); Quote Link to comment https://forums.phpfreaks.com/topic/119137-solved-setting-a-static-variable-in-a-new-class-instance/ Share on other sites More sharing options...
DarkWater Posted August 11, 2008 Share Posted August 11, 2008 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(); Quote Link to comment https://forums.phpfreaks.com/topic/119137-solved-setting-a-static-variable-in-a-new-class-instance/#findComment-613438 Share on other sites More sharing options...
ridiculous Posted August 11, 2008 Author Share Posted August 11, 2008 Yeah I did. And I can't find that anywhere. Quote Link to comment https://forums.phpfreaks.com/topic/119137-solved-setting-a-static-variable-in-a-new-class-instance/#findComment-613442 Share on other sites More sharing options...
ridiculous Posted August 11, 2008 Author Share Posted August 11, 2008 Thank you very, very much. That was incredibly frustrating for me. Quote Link to comment https://forums.phpfreaks.com/topic/119137-solved-setting-a-static-variable-in-a-new-class-instance/#findComment-613443 Share on other sites More sharing options...
DarkWater Posted August 11, 2008 Share Posted August 11, 2008 Okay, no problem, but next time be sure to look a bit harder (the manual has almost everything). Anyway, what were you trying to apply this to? (Like real code) Maybe there's an easier way. Quote Link to comment https://forums.phpfreaks.com/topic/119137-solved-setting-a-static-variable-in-a-new-class-instance/#findComment-613446 Share on other sites More sharing options...
ignace Posted August 11, 2008 Share Posted August 11, 2008 http://www.php.net/manual/en/language.oop5.php it's apparently not that hard, even i could find it Quote Link to comment https://forums.phpfreaks.com/topic/119137-solved-setting-a-static-variable-in-a-new-class-instance/#findComment-613455 Share on other sites More sharing options...
ridiculous Posted August 11, 2008 Author Share Posted August 11, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/119137-solved-setting-a-static-variable-in-a-new-class-instance/#findComment-613459 Share on other sites More sharing options...
ridiculous Posted August 11, 2008 Author Share Posted August 11, 2008 Ignace, I know where to find the PHP.net OO documentation. I just didn't see anywhere where people use a similar technique: new Class ('variable'); Quote Link to comment https://forums.phpfreaks.com/topic/119137-solved-setting-a-static-variable-in-a-new-class-instance/#findComment-613460 Share on other sites More sharing options...
ridiculous Posted August 11, 2008 Author Share Posted August 11, 2008 Find that and then you can coyly try to make me feel stupid. Quote Link to comment https://forums.phpfreaks.com/topic/119137-solved-setting-a-static-variable-in-a-new-class-instance/#findComment-613461 Share on other sites More sharing options...
ignace Posted August 11, 2008 Share Posted August 11, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/119137-solved-setting-a-static-variable-in-a-new-class-instance/#findComment-613469 Share on other sites More sharing options...
DarkWater Posted August 11, 2008 Share Posted August 11, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/119137-solved-setting-a-static-variable-in-a-new-class-instance/#findComment-613470 Share on other sites More sharing options...
ridiculous Posted August 11, 2008 Author Share Posted August 11, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/119137-solved-setting-a-static-variable-in-a-new-class-instance/#findComment-613472 Share on other sites More sharing options...
DarkWater Posted August 11, 2008 Share Posted August 11, 2008 PHP5 >>>>>>>>>>>>>>>>>>>>>>>>>>>> PHP4 for OOP. Yes, that much better. Why would you try to merge something with PDO when PDO works perfectly fine? >_< Quote Link to comment https://forums.phpfreaks.com/topic/119137-solved-setting-a-static-variable-in-a-new-class-instance/#findComment-613474 Share on other sites More sharing options...
ridiculous Posted August 11, 2008 Author Share Posted August 11, 2008 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"; Quote Link to comment https://forums.phpfreaks.com/topic/119137-solved-setting-a-static-variable-in-a-new-class-instance/#findComment-613480 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.