Jump to content

more classes


l0ve2hat3

Recommended Posts

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


?>

Link to comment
https://forums.phpfreaks.com/topic/146567-more-classes/
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/146567-more-classes/#findComment-769455
Share on other sites

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??

 

 

Link to comment
https://forums.phpfreaks.com/topic/146567-more-classes/#findComment-769462
Share on other sites

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');
    }

}

Link to comment
https://forums.phpfreaks.com/topic/146567-more-classes/#findComment-769466
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/146567-more-classes/#findComment-769488
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/146567-more-classes/#findComment-769537
Share on other sites

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.