Jump to content

Quick OOP Question


bigft

Recommended Posts

Hey I've got a quick question regarding a OOP problem.

 

The thing is, is that I have a main file (A), and it has two variables (B, and C) that both hold separate classes, class B is the mysql functions and class C is the error functions. What I need to know is how to access the error function in class C from class B.

 

For example, (i know this is retarded), but in my mysql class, for the error display I have:

 

mysql_query(......) or parent::C->SetError('omgawd....');

 

And I know this doesn't work, but it may set an example on what I'm looking for.

 

Any help would be appreciated.

 

edit ...(Crap sorry, didn't realize there was an OOP forum before posting)

Link to comment
Share on other sites

Well, to access a class, you have to have a reference to it...  So, how would you go about doing that?

 

-You could pass it as an argument to the second class.

class class1 {
     public function Func() {
          echo 'Hi';
     }
}

$c1 = new Class1;

class class2 {
     private $class1;
     public function __construct($class1) {
          $this->class1 = $class1;
     }
     public function DoSomethingWithC1() {
         $this->class1->Func();
      }
}

$c2 = new Class2($c1);
$c2->DoSomethingWithC1();

 

Or, you could always globalize it.  (I would avoid this way.)

class class1 {
     public function Func() {
          echo 'Hi';
     }
}

$c1 = new Class1;

class class2 {
     public function DoSomethingWithC1() {
          global $c1;
          $c1->Func();         
      }
}

$c2 = new Class2($c1);
$c2->DoSomethingWithC1();

 

 

Or, you could have a singleton, allowing you to get the reference from anywhere without passing or globalizing...

(This is an extremely simple example.)

 

function GetInst() {
     static $inst = null;
     if($inst == null) {
          $inst = new Class1;
     }
     return $isnt;
}
//Please note that this always returns the exact same class.


//then, throughout the script, you can just do...
GetInst()->Func();

 

Anyway, hopefully one of those will work for you, and someone else can probably expand on my answer, since I don't know much about OOP.

Link to comment
Share on other sites

I don't think I explained it to well, here's an example of what I'm trying to do:

 

<?php
class class1{
var $c2;
var $c3;
etc...
.
.
}

class class2{

function error($msg){
	return $msg;
	etc....
}

}

class class3{
function query($sql){
	return mysql_query($sql) or error('can\'t run query...');
}
}

$c1 = new class1;
$c1->c2 = class2;
$c1->c3 = class3;
?>

 

What I'm trying to do is use the error function from class2, in class3.

 

Also, I realized that I never extended the classes so I can't use the parent scope (well I think i can't anyways).

Link to comment
Share on other sites

Make them static methods or instantiate new objects for use inside of the class.  IE:

class Settings {
  protected $db;
  protected $user;

  __construct(DB $db, User $user) {
   $this->db = $db;
   $this->user = $user;
  }

  public function ChangeAvatar {
     //do some stuff with $this->db and $this->user
  }
}

 

Then, assuming you have a DB and User class, you could do:

$db = new DB;

$user = new User("DarkWater");

$settings = new Settings($db, $user);

 

Link to comment
Share on other sites

I think using Error as a case is not a good idea to begin with, because you really should be using Exceptions.

 

Corbin provided you with 2 good examples on how to achieve what you would like.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.