PHP面向对象方法的难点
请参见下面的编辑 我正在尝试用PHP进行OOP开发,我真的开始感到头痛或溃疡。我就是不能把我的头脑放在它上面,部分对我来说似乎太不合逻辑了,我也不知道从哪里开始,这真的让我很沮丧,因为我相信努力学习它是值得的,它让我对我的代码有了更好的了解 昨天我花了一整天的时间在互联网上寻找实用的例子和解释性的文章,但现在我觉得这只会让我更加困惑。我需要一些实用的技巧,而不是像这样的例子 如果你问我,更短(更少的代码)和更明显 对我的项目没有兴趣 我想做的是一个简单的投资组合网站。因此,我只需要4页,一个主页,信息页面,一个关于我的工作和联系方式的页面。我使用了三个表PHP面向对象方法的难点,php,oop,Php,Oop,请参见下面的编辑 我正在尝试用PHP进行OOP开发,我真的开始感到头痛或溃疡。我就是不能把我的头脑放在它上面,部分对我来说似乎太不合逻辑了,我也不知道从哪里开始,这真的让我很沮丧,因为我相信努力学习它是值得的,它让我对我的代码有了更好的了解 昨天我花了一整天的时间在互联网上寻找实用的例子和解释性的文章,但现在我觉得这只会让我更加困惑。我需要一些实用的技巧,而不是像这样的例子 如果你问我,更短(更少的代码)和更明显 对我的项目没有兴趣 我想做的是一个简单的投资组合网站。因此,我只需要4页,一个主
客户机
、内容
和媒体
,我想在我的工作页面上使用它们
我在思考我的结构,然后(在我的头脑中,不知道如何实现它)想到了类似的东西
顺便问一下,这有什么意义吗
在我的index.php上,我有以下几行显示菜单(工作正常)$content=newcontent()$内容->顶部菜单($_aGET[0])代码>
我失去它的地方是top显示客户机列表或客户机详细信息的方式,这取决于变量$\u aGET[0]
(保存url)
假设我想显示客户机的详细信息,因此我需要所有三个表的记录,所以通常我会使用两个join,如下所示
$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'
";
但就我所理解的OOP思想而言,我应该为类中定义的每个表都有一个方法(对吗?),但是我将如何连接这些表呢
或者我的“故事”看起来像是:‘老兄,放下它,坚持程序编码’
编辑:
好的,在Fluffeh的帖子之后,我使用了他的例子并对其进行了一些修改以测试它。问题是我确实得到了一些输出,但不是期望的。我的输出如下
ID: CompanyName
C
Desc: C
Media:
:
:
:
:
:
ID:公司名称
C
描述:C
媒体:
:
:
:
:
:
虽然我应该得到(值在数据库中):
ID:公司名称
公司名称
公司名称是一家公司
媒体:
:图1
:图2
:图3
:图4
:图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 "<div><h3>".$this->name."</h3>";
echo "Desc: ".$this->desc."<br>";
echo "Media:<br>";
for($i=0;$i<$this->clientMediaFiles;$i++){
echo $this->clientMedia[$i]->title." : ".$this->clientMedia[$i]->desc."<br>";
}
echo "</div>";
}
}
类媒体{
公共$类型;
公有产权;
公众形象;
公帑$desc;
}
类客户端{
公共名称;
公帑$desc;
}
类客户端详细信息{
私人$clientID;
public$clientName;
公众$clientDesc;
public$clientMedia=array();
public$clientMediaFiles=0;
公共函数构造($myID){
$this->clientID=$myID;
}
公共函数getMyDetails(){
$db=新数据库();
$db->connect();
$db->query(“
选择c_客户端,c_客户端描述
来自客户
其中c_client=“.mysql\u real\u escape\u string($this->clientID)。”
");
foreach($db->getResult()作为$client){
$this->name=$client['c_client'];
$this->desc=$client['c_client_desc'];
}
$db=新数据库();
$db->connect();
$db->query(“
选择ncm.clme\u me\u id、ncm.clme\u cl\u id、cl.c\u id、me.m\u id、me.m\u类型、me.m\u标题、me.m\u描述、me.m\u链接
来自norm_cl_me_rel作为ncm
以cl.c\U id=ncm.clme\U cl\U id上的cl身份左加入客户
左以me.m_id=ncm.clme_me_id上的我的身份加入媒体
其中me.m_id位于(1,2,3,4,5)
");
foreach($db->getResult()作为$media){
$this->clientMedia[$i]=新媒体();
$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++;
}
}
公共函数displayMyResults(){
echo“ID:$this->clientID”;
回显“div>h3>”$this->name./h3>”;
echo“Desc:”.$this->Desc“br>”;
回声“媒体:br>”;
对于($i=0;$iclientMediaFiles;$i++){
echo$this->clientMedia[$i]->title.:“$this->clientMedia[$i]->desc.“br>”;
}
回声“/div>”;
}
}
继续使用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>";
}
}
?>
编辑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)
因此,OOP是一种很好的方法,如果使用MVC和前端控制器模式来学习的话。但是如果你只想要一个有4个页面的简单站点,你真的可以使用过程代码,没有问题。如果您了解drupal,那么它包含90%的过程代码
但是
$db = new Database();
$db->connect();
创建db对象并在每个方法中连接是不可行的
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 "<div><h3>".$this->name."</h3>";
echo "Desc: ".$this->desc."<br>";
echo "Media:<br>";
for($i=0;$i<$this->clientMediaFiles;$i++){
echo $this->clientMedia[$i]->title." : ".$this->clientMedia[$i]->desc."<br>";
}
echo "</div>";
}
}
<?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>";
}
}
?>
<?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);
}
?>
$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++;
}
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)
$db = new Database();
$db->connect();
class Database extends PDO {
private static db = null;
private function __construct() {
parent::__construct(/* here get some conf data*/);
}
public static getDb() {
if(!self::db) self::db = new self();
return self::db;
}
/* here you can add your custom methods like loadMenu etc. */
}
define('TEMPLATE_DIR', __DIR__ . '/templates');
function render($template, $variables) {
extract($variables);
require TEMPLATE_DIR . "/$template.tpl.php";
}
$variables = array('menu_links' => Database::getDb()->getMenu());
render('menu', $variables);
<ul>
<?php foreach($menu_links as $link) : ?>
<li><a href="<?= $link['href'] ?>"><?= $link['title'] ?></a></li>
<?php endforeach; ?>
</ul>
/**
* Add $user to $channel.
* @param Channel $channel
* @param User $user
*
* @throws Exception
*/
protected function addUser(Channel $channel, User $user) {
if (!isset($this->chanList[$channel->name])) { #Channel is not on channel list
throw new Exception("You are not on that channel");
}
if (!isset($this->userList[$user->nick])) { #User is not on global user list (New user)
$this->userList[$user->nick] = $user; #Add user to list
}
$this->chanList[$channel->name]->addUser($user); #Add the user to the channel user list
}