Jump to content

[SOLVED] New to OOP, question about interaction


jrws

Recommended Posts

Sorry, but I am new to classes and OOP. I was wondering how do you get two classes to interact with each other?

For example I have the mysql class that does mysql functions, and one of them is to count the rows. Then I have a user class that needs to use this function to see if the user exists. How do I do this? Do I use the double :: or?

Any help is much appreciated.

Link to comment
Share on other sites

You use :: to access static methods, and -> to access instance methods.

 

If you have DB class, you can have DB object reference as a property in user class:

public $db;

 

You instantiate the DB object in the user class constructor:

$this->db = new DB();

 

But, you don't want to have multiple connections to the datebase,

so you can use design pattern called singleton.

In short, you have only one DB object and you don't have to pass it from one method to another.

There are other options, it is just one of them for DB object.

 

Read more about singleton design pattern, and patterns in general.

If you have any questions you can ask.

 

Also check PDO, it's probably better than building your own DB class:

http://il.php.net/manual/en/book.pdo.php

Link to comment
Share on other sites

You could always use inheritance.

 

class foo
{
      function test( $string )
      {
             return $string;
      }
}
class bar extends foo
{
      function echoString( $string )
      {
             echo $this-> test( $string );
      }
}

 

So basically, whenever you extend a class, it makes the functions inside the class that you are extending available :).

 

Hope that makes sense :).

 

Link to comment
Share on other sites

@Mikedean: Not really.  Inheritance should not be used in this case.  Not at all.  Inheritance is really one of the most abused things in OOP. =P

 

@Thread starter: If your User class is directly using a DB instance, you're doing it wrong.  Honestly.  Go read up on design patterns and OOP principles.  Before you go recoding procedural code directly into useless OOP functions, learn how and why OOP should be used.  In reality, a user (remember, OOP was made to model real world situations) would not go contact the database to see if they existed.  A User has nothing to do with a Database, so they should not be coupled like that.

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.