Author Topic: [SOLVED] New to OOP, question about interaction  (Read 494 times)

0 Members and 1 Guest are viewing this topic.

Offline jrwsTopic starter

  • Enthusiast
  • Posts: 123
    • View Profile
[SOLVED] New to OOP, question about interaction
« on: December 26, 2008, 03:02:17 AM »
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.

Offline Sagi

  • Irregular
  • Posts: 5
    • View Profile
Re: New to OOP, question about interaction
« Reply #1 on: December 26, 2008, 06:10:53 AM »
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

Offline Mikedean

  • Enthusiast
  • Posts: 78
  • Gender: Male
    • View Profile
    • Essex Web Development
Re: New to OOP, question about interaction
« Reply #2 on: December 26, 2008, 09:18:56 AM »
You could always use inheritance.

Code: [Select]
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 :).
Freelance Web Development Services

PHP, Active Server Pages (Classic ASP), C# (.Net), MySQL, SQL Server, HTML, CSS + JavaScript.

Offline DarkWater

  • Freak!
  • Posts: 6,158
  • Gender: Male
    • View Profile
Re: New to OOP, question about interaction
« Reply #3 on: December 26, 2008, 12:50:58 PM »
@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.
« Last Edit: December 26, 2008, 12:53:14 PM by DarkWater »
Info:Apache 2.2.4 | PHP 6.0.0-dev, PHP 5.2.4 | Ubuntu 8.04 | Age: 16 | Coding PHP and CSS+(X)HTML: 5-6 years | Feel free to PM me if you need help!
Αν χρειάζεσαι ένας μεταφραστής, μπορέις να μου μιλάς.  Μιλώ καλά.
Quote from: Crayon Violent
If you ask for a banana, would you expect someone to hand you an orange? Or a fork?  No you wouldn't, because you asked for a banana, and you expect them to understand that a banana is not an orange or a fork.  It's the same principle.

Offline hobeau

  • Enthusiast
  • Posts: 62
  • Gender: Male
    • View Profile
    • Solutionbot
Everything should be as simple as possible,, but not one bit simpler.

Offline jrwsTopic starter

  • Enthusiast
  • Posts: 123
    • View Profile
Re: New to OOP, question about interaction
« Reply #5 on: December 26, 2008, 08:52:42 PM »
Thanks for the help. I will try to do that then.