Omar Juvera
Omar Juvera

Reputation: 12287

class within another class

I am new to PHP OOP and would like to try to nest several classes within another class, to latter call them like so:

$sql = new SQL();
$sql->Head()->Description($_SESSION['page']);
  //OR
$sql->Head()->Keywords($_SESSION['page'])
  //OR
$sql->Body()->Clients($_SESSION['client'])
  //ETC
$query = $sql->Run(); // equivalent to mysql_query("...");

As you can guess, I run into some problems and ended with this poor code:

<?php
require( $_SERVER['DOCUMENT_ROOT'] . '/#some_db_directory/database.php');
//This file contains $db['host'], $db['user'], etc...

class SQL {
    public $sql;

    public function __construct() {
        global $db;
    }

    public class Head() {

        public function Description($page) {
            return "SELECT * FROM `$db['database']`.`desciption` WHERE `page` = '$page'";
        }

        public function Keywords($page) {
            return "SELECT * FROM `$db['database']`.`keywords` WHERE `page` = '$page'";
        }
    }
}

$sql = new SQL();
echo $sql->Head()->Description('home'); //For testing

Upvotes: 0

Views: 141

Answers (3)

Kami
Kami

Reputation: 19407

Try it like this

<?php
require( $_SERVER['DOCUMENT_ROOT'] . '/#some_db_directory/database.php');
//This file contains $db['host'], $db['user'], etc...

class SQL {
    public $sql;
    private $_head;

    public function __construct() {
        global $db;
        $_head = new HeadClass();
    }

    public function Head() {
        return $this->_head;
    }
}

class HeadClass { // Class cannot have a public access modifier

    public function Description($page) {
        return "SELECT * FROM `" . $db['database'] . "`.`desciption` WHERE page = $page";
    }

    public function Keywords($page) {
        return "SELECT * FROM `" . $db['database'] . "`.`keywords` WHERE page = $page";
    }
}

$sql = new SQL();
echo $sql->Head()->Description('home.html');
?>

I am moving the class declaration outside the class and creating an instance of the class with in SQL. This is then made available via the Head() function.

Note: For body you will need to create a separate class and use a reference in the SQL class to it like I have done for head.

Upvotes: 0

Pastor Bones
Pastor Bones

Reputation: 7351

I'm assuming that database.php is a database class. In that case you could do something like this.

head.php

Class Head{
    private $_db;
    private $_dbName;

    public function __construct($db, $dbName){
        $this->_db = $db;
        $this->_dbName = $dbName;
    }
    public function Description($page) {
       $results = $this->_db->query("SELECT `text` FROM `$this->_dbName`.`description` WHERE `page` = '$page'");
       return '<meta name="description" content="' . $results['text'] . '">';
    }

    public function Keywords($page) {
       $results = $this->_db->query("SELECT * FROM `$this->_dbName`.`keywords` WHERE `page` = '$page'");
       $keywords = array();
       foreach($results as $result){
           array_push($keywords, $result['word']);
       }
       return '<meta name="keywords" content="' . implode(',', $keywords) . '">';
    }
}

sql.php

require( $_SERVER['DOCUMENT_ROOT'] . '/#some_db_directory/database.php');
// Require head class file
require( $_SERVER['DOCUMENT_ROOT'] . '/#some_db_directory/head.php');   

Class SQL{
    public $Head;

    public function __construct($dbName){
        global $db;
        $this->Head = new Head($db, $dbName);
    }
}

You would then pass the name of the database into the SQL class (which propogates through to the Head class).

// Require the sql class file
require( $_SERVER['DOCUMENT_ROOT'] . '/#some_db_directory/sql.php');
$sql = new SQL('mydatabase');
echo $sql->Head->Description('home');

Again note that your database class might not return results the way I'm using them here. You will have to modify this to work with your particular database class.

Upvotes: 1

Muthu Kumaran
Muthu Kumaran

Reputation: 17900

What you are trying to do is called Encapsulation. Try google search on PHP encapsulation to learn more.

Here is a code example from http://www.weberdev.com/get_example.php3?ExampleID=4060,

<?php 

class App { 

     private static $_user; 

     public function User( ) { 
          if( $this->_user == null ) { 
               $this->_user = new User(); 
          } 
          return $this->_user; 
     } 

} 

class User { 

     private $_name; 

     public function __construct() { 
          $this->_name = "Joseph Crawford Jr."; 
     } 

     public function GetName() { 
          return $this->_name; 
     } 
} 

$app = new App(); 

echo $app->User()->GetName(); 
?>

Upvotes: 1

Related Questions