instantiating a class within a method of a seperate class

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
d-rave
Forum Newbie
Posts: 1
Joined: Thu Feb 06, 2003 5:44 am
Location: chester

instantiating a class within a method of a seperate class

Post by d-rave »

Can I initiate a class from within a method of a seperate class

For example, below is a class called Detect that has an attribute called $oBudDb, which I'd hoped to use in further methods within the Detect class. This attribute is assigned to another class seperate to Detect.
This other class, Remote_ibisdb1_MsSQL(), extends a Class DB_MsSQL().


However, when I debug this on ZDE a debug error is displayed:
Debug Error: ..\Class DB_MsSQL.inc line 116 - Call to undefined function: mssql_connect()

Can anyone help?

Might it be that an extended class cannot be called from within another class?




$user = new Detect( $_SESSION["ibis_username"] );
....
....
class Detect {

var $oBudDb; // database handling class
var $found;
var $loginID; // UserID
var $shtName; // USerName
var $fstName; // Firstname
var $LstName; // Surname
var $usrDept; // UserDept
var $scopeID; // UserAccess

var $depCode; // DeptCode
var $depDesc; // DeptDesc
var $depHldr; // BudHldr
var $depMngr; // LineMgr
var $ibisRef; // ibis_name

function Detect( $ibisRef ){

if( !class_exists( $oBudDb ) ) $oBudDb = new Remote_ibisdb1_MsSQL(); // Extension of DB_MsSQL with connects to a server and selects the database

$oBudDb->Query( "SELECT UserID, UserName, Surname, Firstname, UserDept, UserAccess ".
"FROM BudgetReport ".
"WHERE UserID = '".$_SESSION["ibis_username"]."' ".
"AND UserAccess IS NOT NULL ".
"GROUP BY UserID, UserName, Surname, Firstname, UserDept, UserAccess " );


The above line and this one have worked in similar circumstances but zend debugger seems to have a problem.
User avatar
riley
Forum Commoner
Posts: 45
Joined: Thu May 02, 2002 6:31 pm

Post by riley »

Hopefully someone will give you a much better answer, BUT I don't think you can do that. I have an error catching class and would much like to use it with a database abstraction layer class that I have started with no luck. I believe it is by the way the inheritance works in php.
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post by Stoker »

I think you may be missunderstanding the concepts of OO a little bit, a class is just a definition, or structure.. An Object is an instance of a class, nothing inside a class is "alive" until it is instatinated.. However, it is possible to call methods in a class, however, this method will not have any data from itself.. class_exist() s will check if the class has been defined, not if there has been any objects instatinated from that class.

Using OO in PHP is not very efficient, not recommended for high-volume sites unless you need a lot of dynamical behaviour, building data trees and such.. If you find yourself using globals and such, the whole point of OO is lost..

here is a simple and silly sample of (untested) a class inheritance and an object holding other objects..

Code: Select all

<?php
class beverage {

   var $level;
   var $brand;
   var $type;

  function buy () {
     $this->level++;
  }

  function drink () {
      if ($this->level) $this->level--;
      else return 'None left';
  }
}

class beer extends beverage {
   function beer ($brand)  {
      $this->type = 'beer';
      $this->brand = $brand;
   }
}

class party {

   var $beverage;

   function buy_beer ($amount,$brand) {
      $this->beverage['beer'][$brand] = new beer($brand);
      while ($amount--) $this->beverage['beer'][$brand]->buy();
   }

   function drink_beer ($brand) {
      if (is_object($this->beverage['beer'][$brand])) {
        return $this->beverage['beer'][$brand]->drink());
      }
   }
}

$spring_break = new party;
$spring_break->buy_beer(24,'Sam Adams');  # Just as if  $spring_break->beverage['beer']['Sam Adams']->level == 24
$spring_break->drink_beer('Sam Adams');

?>
Post Reply