l0ve2hat3 Posted February 23, 2009 Share Posted February 23, 2009 how do i access mssql_value in db_manager class, if i am in pca_inventory class??? <?php class db_manager{ public $dbname="gnglatt"; public $host="MARS\PCAMERICA"; public $port="1092"; public $user="demo"; public $password="demo"; public $connection; function _connect(){ $this->connection=mssql_connect($this->host,$this->user,$this->password); mssql_select_db($this->dbname,$this->connection); } public function check_connect(){ if(!$this->connection)$this->_connect(); } public function query($query){ $this->check_connect(); $qresult=mssql_query($query) or Die($query); return $qresult; } public function mssql_value($table,$field,$id,$idfield="id"){ $sql='SELECT ['.$field.'] FROM ['.$table.'] WHERE ['.$idfield.']="'.$id.'"'; $value=mssql_result($this->query($sql),0,$field); return $value; } } class pca_inventory{ public function get_price($item_number){ return db_manager::mssql_value('Inventory','Price',$item_number,'ItemNum'); } } $item = new pca_inventory; echo $item->get_price('LUNCHSPEC'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/146567-more-classes/ Share on other sites More sharing options...
l0ve2hat3 Posted February 23, 2009 Author Share Posted February 23, 2009 **how do i access db_manager::mssql_value(), if i am in pca_inventory class??? Quote Link to comment https://forums.phpfreaks.com/topic/146567-more-classes/#findComment-769452 Share on other sites More sharing options...
rhodesa Posted February 23, 2009 Share Posted February 23, 2009 your concept of classes is still off. i've sat her trying to type out an explanation, but can't seem to come up with something. there is something about classes where it is so hard to explain to someone else. did you go through that tutorial I posted in your other thread? Quote Link to comment https://forums.phpfreaks.com/topic/146567-more-classes/#findComment-769455 Share on other sites More sharing options...
Maq Posted February 23, 2009 Share Posted February 23, 2009 You can use extends if you want to invoke methods from another class. class pca_inventory extends db_manager{ Quote Link to comment https://forums.phpfreaks.com/topic/146567-more-classes/#findComment-769456 Share on other sites More sharing options...
l0ve2hat3 Posted February 23, 2009 Author Share Posted February 23, 2009 yeah i went through the tutorial... i know i could use extends but i thought u might be able to do this.. class pca_inventory{ public $db=new db_manager; public function get_price($item_number){ return $db->mssql_value('Inventory','Price',$item_number,'ItemNum'); } } no go... rhodesa- any way to try to tell me what i am doing wrong with the concept?? Quote Link to comment https://forums.phpfreaks.com/topic/146567-more-classes/#findComment-769462 Share on other sites More sharing options...
l0ve2hat3 Posted February 23, 2009 Author Share Posted February 23, 2009 well this works, but y cant i make $db public?? so confused class pca_inventory{ public function get_price($item_number){ $db=new db_manager; return $db->mssql_value('Inventory','Price',$item_number,'ItemNum'); } } Quote Link to comment https://forums.phpfreaks.com/topic/146567-more-classes/#findComment-769466 Share on other sites More sharing options...
KevinM1 Posted February 23, 2009 Share Posted February 23, 2009 well this works, but y cant i make $db public?? so confused class pca_inventory{ public function get_price($item_number){ $db=new db_manager; return $db->mssql_value('Inventory','Price',$item_number,'ItemNum'); } } Why would you want it to be public? In the vast, vast majority of cases, class properties should be private. Quote Link to comment https://forums.phpfreaks.com/topic/146567-more-classes/#findComment-769488 Share on other sites More sharing options...
l0ve2hat3 Posted February 23, 2009 Author Share Posted February 23, 2009 i guess your right... but im bothered now... what am i missing about my concept of classes?? Quote Link to comment https://forums.phpfreaks.com/topic/146567-more-classes/#findComment-769490 Share on other sites More sharing options...
KevinM1 Posted February 23, 2009 Share Posted February 23, 2009 i guess your right... but im bothered now... what am i missing about my concept of classes?? Well, it's hard to tell from looking at a couple of code snippets.... Let's start with what you do know, and work from there. Quote Link to comment https://forums.phpfreaks.com/topic/146567-more-classes/#findComment-769501 Share on other sites More sharing options...
gizmola Posted February 23, 2009 Share Posted February 23, 2009 As you've seen there is no problem instantiating an object of class A inside of class B. You simply do this type of work inside a method. You can't declare a class variable and instantiate an object in the declaration, but that's simply syntax. Quote Link to comment https://forums.phpfreaks.com/topic/146567-more-classes/#findComment-769537 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.