PHP OOP方法difficultiues

Mau*_*ice 5 php oop

请参阅下面的编辑

我正试图深入研究PHP中的OOP开发,我真的开始出现头痛或溃疡.我只是无法绕过它,部分看起来对我来说太不合逻辑了,我不知道从哪里开始它真的让我感到沮丧,因为我相信值得努力尝试学习它并且它提供了我更好的概述关于我的代码.

昨天我花了一整天的时间在互联网上搜索实用的例子和解释性文章,但现在我觉得这只会让我更加困惑.我需要一些实用的技巧,而不是像


class person {
     var $name;
     function set_name($new_name) {
          $this->name = $new_name;
     }
     function get_name() {
          return $this->name;
     }
}
$stefan = new person();
$jimmy = new person;

$stefan->set_name("Stefan"); $jimmy->set_name("Jimmy");

echo "Stefan's full name: " . $stefan->get_name(); echo "Jimmy's full name: " . $jimmy->get_name();

像这样的例子让我怀疑为什么我不应该(因为它都在同一页面中定义)

$name = "Stefan"; 
echo "Stefan's full name: ".$name;
Run Code Online (Sandbox Code Playgroud)

如果你问我,更短(更少的代码),更明显?

不,我的项目.

我想做的是一个简单的投资组合网站.因此,我只需要4页,主页,信息页面,关于我的工作的页面和联系表格.我用三个表clients,contentmedia我想在上使用我的工作页面.

我正在考虑我的结构,并且出现了(在我脑海中,不知道如何实现它)

class Database{}
class Content extends Database{}
     function showMenu(){}
     function showContent{}
class Clients extends Database{}
     function showClientList{}
     function showClientDetails{}
class Media extends Database{}

我已经创建了一个数据库类,它使用公共方法建立数据库连接query($qry).我从Content课程开始,现在看起来像这样:


class Content extends Database {
    public function topMenu($page){
        $db = new Database();
        $db->connect();
        $db->query("SELECT m_link, m_title, m_accesskey FROM menu WHERE m_type = 'top'"); 
        $res = $db->getResult();
        foreach($res AS $show){
            if($page == $show['m_link']){
                echo("<li id="active">$show['m_title']."</li>\n");
            }else{
                echo("<li>$show['m_title']."</li>\n");
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

顺便说一句,这有什么意义吗?

在我的index.php上,我有以下行显示菜单(工作正常) $content = new Content(); $content->topMenu($_aGET[0]);

我放弃它的方式是顶部显示客户列表客户端详细信息的方式,这取决于变量$_aGET[0](保存网址)

假设我想显示客户端详细信息,因此我需要来自所有三个表的记录,所以通常我会使用几个连接,如此

$query = "
    SELECT cl.c_id, cl.c_client, cl.c_client_desc, ncc.clco_cl_id, ncc.clco_co_id, co.c_content, ncm.clme_me_id, ncm.clme_cl_id, GROUP_CONCAT(me.m_link) AS images_link
    FROM clienten AS cl
    LEFT JOIN norm_cl_co_rel AS ncc ON cl.c_id = ncc.clco_cl_id
    LEFT JOIN content AS co ON ncc.clco_co_id = co.c_id
    LEFT JOIN norm_cl_me_rel AS ncm ON cl.c_id = ncm.clme_cl_id
    LEFT JOIN media AS me ON me.m_id = ncm.clme_me_id
    WHERE cl.c_client = '".mysql_real_escape_string($_aGET[1])."'
    AND cl.c_language_id = '"._LANGUAGE."' 
    AND cl.c_active = '1'
";
Run Code Online (Sandbox Code Playgroud)

但到目前为止我理解OOP的想法,我应该为一个类中定义的每个表有一个方法(对吧?)但是我怎么加入这些?

或者我的"故事"看起来像是什么:'老兄,只是放弃它并坚持使用程序编码'?

编辑:

好吧,在Fluffeh的帖子之后,我使用他的例子并对其进行了一些修改以测试它.问题是我确实得到了一些输出,但不是所希望的.我的输出如下

ID: CompanyName
C
Desc: C
Media:
: 
: 
: 
: 
:

我应该得到(值在数据库中):

ID: CompanyName
CompanyName
Desc: CompanyName is a company
Media:
: Image 1
: Image 2
: Image 3
: Image 4
: Image 5

我的代码看起来像这样:

class media{
    public $type;
    public $title;
    public $image;
    public $desc;
}

class client{
    public $name;
    public $desc;
}

class clientDetails{

    private $clientID;
    public $clientName;
    public $clientDesc;
    public $clientMedia = array();
    public $clientMediaFiles = 0;

    public function __construct($myID){
        $this->clientID = $myID;
    }

    public function getMyDetails(){

        $db = new Database();
        $db->connect();
        $db->query("
            SELECT c_client, c_client_desc 
            FROM clienten 
            WHERE c_client = '".mysql_real_escape_string($this->clientID)."'
        ");
        foreach($db->getResult() as $client){
            $this->name = $client['c_client'];
            $this->desc = $client['c_client_desc'];
        }

        $db = new Database();
        $db->connect();
        $db->query("
            SELECT ncm.clme_me_id, ncm.clme_cl_id, cl.c_id, me.m_id, me.m_type, me.m_title, me.m_desc, me.m_link
            FROM norm_cl_me_rel AS ncm
            LEFT JOIN clienten AS cl ON cl.c_id = ncm.clme_cl_id
            LEFT JOIN media AS me ON me.m_id = ncm.clme_me_id
            WHERE me.m_id IN(1,2,3,4,5)
        ");
        foreach($db->getResult() as $media){
            $this->clientMedia[$i]= new media();
            $this->clientMedia[$i]->type = $media['m_type'];
            $this->clientMedia[$i]->title = $media['m_title'];
            $this->clientMedia[$i]->image = $media['m_image'];
            $this->clientMedia[$i]->desc = $media['m_desc'];
            $this->clientMediaFiles++;
        }
    }

    public function displayMyResults(){
        echo "ID: $this->clientID";
        echo "&lt;div>&lt;h3>".$this->name."&lt;/h3>";
        echo "Desc: ".$this->desc."&lt;br>";
        echo "Media:&lt;br>";
        for($i=0;$i<$this->clientMediaFiles;$i++){
            echo $this->clientMedia[$i]->title." : ".$this->clientMedia[$i]->desc."&lt;br>";
        }
        echo "&lt;/div>";
    }

}
Run Code Online (Sandbox Code Playgroud)

Flu*_*feh 6

继续使用OOP.一旦你得到它,你将永远不想回到讨厌,麻烦,讨厌,可怕,狂热的程序代码.

举个例子:假设我想显示客户端的详细信息,因此我需要来自所有三个表的记录,所以通常我会使用几个连接,就像这样......

这是一个很好的例子,可以用来向您展示OOP的有用性.

ClientDetails创建一个类

  • 在其中创建一个接受clientID作为参数的构造函数.
  • 在这个构造函数中,让它运行代码并使用查询返回的数据填充它的内部变量.
  • 创建一个函数,输出返回到一个漂亮格式的数据.
  • 现在从主代码创建它并让它显示它的详细信息

突然间,您在类中有几行代码,但可以在两行代码中完美地显示您的数据.

我将很快添加一些示例代码,看看我是否可以翻译我的一些类来进行演示.

编辑:该死的,没有翻过旧的类,对此非常详细,但我写了一些代码示例:

<?php

class clientContact
{
    public $name;
    public $phone;
}

class clientDetails
{
    private $clientID;      //only this class can access this variable.
    public $clientName;     // Anything can change these.
    public $clientPhone;
    public $additionalContacts= array();
    public $associates=0;

    public function __construct($myID)
    // This is a magic function that is called every time we make a new object of this class.
    {
        $this->$clientID=$myID;
    }

    public function getMyDetails()
    // This function will query the database to get it's information properly
    // and populate itself with everything it needs. You could do this as part
    // of the __construct() call, but I didn't want to do TOO much code.
    {
        $sql="select clientName, clientPhone from clients where clientID=".$this->clientID.";";
        // skipping over full SQl bits...
        foreach($stmt as $clientContact)
        {
            $this->clientName=$clientContact->clientName;
            $this->clientPhone=$clientContact->clientPhone;
        }

        $sql="select clientName, clientPhone from associates where assocID=".$this->clientID.";";
        // skipping over full SQl bits...
        foreach($stmt as $clientContact)
        {
            $this->additionalContacts[$i]= new clientContact();
            $this->additionalContacts[$i]->name=$clientContact->name;
            $this->additionalContacts[$i]->phone=$clientContact->phone;
            $this->associates++;
        }

    }

    public function displayMyResults()
    // This function will simply display everything it has in a nicely format
    {
        echo "<div><h3>".$this->clientName."</h3>";
        echo "My contact phone number is :".$this->phone."<br>";
        echo "My associates are:<br>";
        for($i=0;$i<$this-associates;$i++)
        {
            echo $this->additionalContacts[$i]->name." : ".$this->additionalContacts[$i]->phone."<br>";
        }
        echo "</div>";

    }
}

?>
Run Code Online (Sandbox Code Playgroud)

天哪,那是长而可怕的吧?

并不是的.这就是原因.

当然,课程中有一些代码,但现在,您可以在主页上执行以下操作:

<?php 

    $someArray=array(36, 25, 76); 
    // This could be from a query, or a form, or anything else

    for($i=0;$i<count($someArray);$i++)
    {
        $currentContact= new clientDetails($someArray[$i]);
        // This is how we create the object.

        $currentContact->getMyDetails();
        // This now tells the object to get it's own data

        $currentContact->displayMyResults();
        // This now displays the data in the object to the page.
        echo "<hr>";
        unset($currentContact);
    }
?>
Run Code Online (Sandbox Code Playgroud)

使用TINY代码片段,您只需显示所有联系人,按照您的需要进行格式化,您只需完全按照您的需要显示所有联系人.

现在想象一下,不是像这样的一个小例子,而是将这些对象用于所有内容.论坛上的帖子?一个对象.业务关键绩效指标的复杂计算?一个对象可以获取它拥有的所有数据,然后计算预测,将结果显示在图表中并通过电子邮件发送给需要它的用户.

当你来自HTML和PHP脚本的背景从上到下执行时,OOP可以有一个陡峭的学习曲线,但是当你开始掌握它时,你会希望你从一开始就学会了它.

编辑:

m_image的查询中似乎没有该列:

    $db = new Database();
    $db->connect();
    $db->query("
        SELECT cl.c_id, ncm.clme_me_id, ncm.clme_cl_id, me.m_type, me.m_title, me.m_desc, me.m_link, m_image
        FROM clienten AS cl
        LEFT JOIN norm_cl_me_rel AS ncm ON cl.c_id = ncm.clme_cl_id
        LEFT JOIN media AS me ON me.m_id = ncm.clme_me_id
        WHERE me.m_id IN(1,2,3,4,5)
    ");
    foreach($db->getResult() as $media){
        $this->clientMedia[$i]= new media();
        $this->clientMedia[$i]->type = $media['m_type'];
        $this->clientMedia[$i]->title = $media['m_title'];
        $this->clientMedia[$i]->image = $media['m_image'];
        $this->clientMedia[$i]->desc = $media['m_desc'];
        $this->clientMediaFiles++;
    }
Run Code Online (Sandbox Code Playgroud)

编辑2:我不是100%肯定,但可能是返回的字段名称.尝试用以下代码替换查询:

SELECT cl.c_id as c_id, ncm.clme_me_id as clme_me_id, ncm.clme_cl_id as clme_cl_id, me.m_type as m_type, me.m_title as m_title, me.m_desc as m_desc, me. as m_link
FROM clienten AS cl
LEFT JOIN norm_cl_me_rel AS ncm ON cl.c_id = ncm.clme_cl_id
LEFT JOIN media AS me ON me.m_id = ncm.clme_me_id
WHERE me.m_id IN(1,2,3,4,5)
Run Code Online (Sandbox Code Playgroud)