Jump to content

Is this possible?


Aureole

Recommended Posts

I have a member, render and database class. I want to be able to use the database class within the other two. Is this possible? Would I maybe do something like...

 

class database extends member, render {

 

I'm not sure but I need to use the database class within my other classes and yes it would extend the other classes and provide them with functionality, right now when trying to use my database class in another class I get the dreaded call to a member function on a non-object.

 

Link to comment
https://forums.phpfreaks.com/topic/73229-is-this-possible/
Share on other sites

Ok just so I understand correctly...

 

My member class looks kind of like this...

 

<?php
class member {
    var $c_time;
    var $update;
    var $setwhat;
    var $setequals;
function last_action() {
            $this->update = $update;
            $this->setwhat = $setwhat;
            $this->setequals = $setequals;
            $this->c_time = time();
	$query = "UPDATE `$this->update` SET mem_last_action='{$this->c_time}' WHERE $this->setwhat='{$this->setequals}'";
	$result = mysql_query($query);
}
}
?>

 

So to do it your way I would do, this:

 

<?php
class member {
    var $c_time;
    var $update;
    var $setwhat;
    var $setequals;
    private $database = false;
    public function last_action($update, $setwhat, $setequals) {
        $this->update = $update;
        $this->setwhat = $setwhat;
        $this->setequals = $setequals;
        $this->database = new database;
        $this->c_time = time();
        $database->simpleupdate('member', 'mem_last_action', $this->c_time, $_SESSION['mem_id']);
}
}
?>

 

Also I would need to instantiate database first, correct?

Link to comment
https://forums.phpfreaks.com/topic/73229-is-this-possible/#findComment-369445
Share on other sites

You would probably be better of instantiating the db object within the member objects constructor. This way you can easily use it within any method  it is required.

 


<?php

class member {
  
  var $c_time;
  var $update;
  var $setwhat;
  var $setequals;

  function __construct() {
    $this->database = new database;
  }

  public function last_action($update, $setwhat, $setequals) {
    $this->update = $update;
    $this->setwhat = $setwhat;
    $this->setequals = $setequals;
    $this->c_time = time();
    $this->database->simpleupdate('member', 'mem_last_action', $this->c_time, $_SESSION['mem_id']);
   
  }

}

?>

 

Its a bit hard to tell if your using php5 or 4, but Ive given an example in php5.

Link to comment
https://forums.phpfreaks.com/topic/73229-is-this-possible/#findComment-369459
Share on other sites

I personally prefer to pass objects to classes (instead of creating new objects all over shop and the overhead that goes with it)

 

SO

 

$db = new database;

 

$member = new member;

$member->$db = $db;

 

$render = new render;

$render->$db = $db;

 

then inside the render and member classes use $this->db->   

Link to comment
https://forums.phpfreaks.com/topic/73229-is-this-possible/#findComment-369945
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.