views:

68

answers:

6

Hello

I've been into this problem for a while already, and have asked some questions about it here Stackoverflow. I've got some advice, but I just can't understand it. Could someone provide me an example of classes working smoothly together.

I have 3 (maybe more) classes:

  • mysql
  • user
  • alerts

As I said, could someone provide an example, so these classes could use functions from each other class, e.g. user could use mysql's functions. I'm asking for an simple example, for learning-purposes.

And please, no google-suggestions or links to other questions. I've tried to search this for a while already. No success, though.

Martti Laine

+1  A: 

It's hard to understand exactly what you mean when you say "working smoothly together". Classes can be used together in a myriad of ways. If they couldn't be then object oriented programming wouldn't be much good.

Here is a simple example:

class mysql {
    private $alert;
    public function __construct(alerts $alert) {
        $this->alert = $alert;
    }

    public function dosomething() {
        if(/* something went wrong */ ) {
            $this->alert->showAlert();
        }
    }
}
Daniel Bingham
A: 

There are two way you can do it.

1st: Use static methods

<?php
class mysql_database
{
    public static function query($q)
    {
        return mysql_query($q);
    }
}

class user
{
    public function get()
    {
        //calling static method from another class
        return mysql_database::query("SELECT * FROM users");
    }
}
?>

2nd: Give objects instances as a parameters to other objects methods

<?php
class mysql_database
{
    public function query($q)
    {
        return mysql_query($q);
    }
}

class user
{
    public function get($DB)
    {
        //calling method using given instance
        return $DB->query("SELECT * FROM users");
    }
}

$DB = new mysql_database();
$user = new user();
$user->get($DB);
?>
Silver Light
+3  A: 

I really recommend you read about classes first - http://php.net/manual/en/language.oop5.php because these are basic concepts if you don't understand a single code example won't help you much.

class Mysql {
    // Public function accessible from everywhere, with class instance
    public function hello() {
        echo '<br>Mysql says Hello';
    }
    // Static function accesible from everywhere, without class instance
    static function bye() {
        echo '<br>Mysql says Bye';
    }
}

class User {
    // Public function accessible from everywhere, with class instance
    public function hello() {
        $mysql = new Mysql();
        $mysql->hello();
        Mysql::bye();
    }
}

class Alert {
    // Static function accesible from everywhere, without class instance
    static function hello() {
        $user = new User();
        $user->hello();
    }
}

$user = new User();
$user->hello();

Mysql::bye();

Alert::hello();
Ivo Sabev
Thanks, in fact I know something about classes, but not that much :D Nice answer, though :)
Martti Laine
A: 

You can smoothly :

  • Instanciate an object from one class within another one
  • Pass an object instance into another one : this is called dependency injection
  • Use static function calls

In all big PHP apps, I see a mix of all 3. Their use depends of the whole design of application, usage, refactoring an testability needs, etc.

Benoit
A: 

Classes should be working together to achieve a desired result. If you are looking for dependency injection in particular or other methods they are explained in most OOP literature.

But if let's say your Mysql Class exposes a number of functions that will be used by your user class you could inject an instance of the Mysql Class into your user class upon instanstiation:

class User {

private $db = null;

public function __construct($mysql) {
  $this->db = $mysql
}

public function getUserName($userID){ 
   $sql = "SQL_QUERY";
   $result = $this->db->ExecuteQuery($sql);
}

}

Please make sure you read the CONS of Dependency Injection and understand WHY this method is preferred over others. If you which to change your Mysql Class to DBClass and not break your existing code you will have to implement the same methods. Of cousre this can get more "complicated" so a careful Design might be needed (your classes might have to extend abstract classes or implement interfaces)....

I suggest you spend some time on the literature and study some patterns to get an overall idea..it's enough to get you started (a good starting point in my opinion)

andreas
A: 

following is another example.

class MySql
{
   private function query($q)
   {
      return mysql_query($q);
   }

   public function checkCredentials($user, $pass)
   {
      $query = "" // add code here to check the user credentials
      $result = query($query);

      $outcome = <true/false>;//based on however you analyze your result
      return $outcome;
   }
} 

class alert
{
  public function generateAlert($alert)
  {
    //code to display your alert
  }
}

class user
{
  var $mysql;
  var $alert;

  public function __construct($mysql, $alert)
  {
    $this->mysql = $mysql;
    $this->alert = $alert;
  }

  public function checkLogin($user, $pass)
  {
    if (!$this->mysql->checkCredentials($user, $pass))
       $this->alert->generateAlert("login failed!");
  }
}

There are many ways of doing object oriented design, it really depends on what the requirements for your project are. I also recommend visiting the PHP site and looking their OOP tutorials.

Nertim