0

我是 PHP OOP 的新手,想尝试在另一个类中嵌套几个类,然后像这样调用它们:

$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("...");

你可以猜到,我遇到了一些问题,并以这个糟糕的代码结束:

<?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
  • 是否可以在 PHP 中嵌套类?
  • 如果是这样,它是如何完成的?
4

3 回答 3

1

你想要做的事情叫做Encapsulation. 尝试 google 搜索PHP 封装以了解更多信息。

这是来自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(); 
?>
于 2012-11-13T11:01:01.253 回答
1

我假设这database.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);
    }
}

然后将数据库的名称传递给 SQL 类(传播到 Head 类)。

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

再次注意,您的数据库类可能不会以我在这里使用它们的方式返回结果。您将不得不修改它以使用您的特定数据库类。

于 2012-11-13T11:01:14.833 回答
0

像这样试试

<?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');
?>

我正在将类声明移到类之外并使用 in创建类的实例SQL。然后通过该Head()功能使其可用。

注意:对于 body,您需要创建一个单独的类并在 SQL 类中使用对它的引用,就像我对 head 所做的那样。

于 2012-11-13T11:00:56.393 回答