English 中文(简体)
合并两个 PHP 对象(类)
原标题:Combine two PHP objects (classes)

我正在研究一个需要很多不同对象(类)才能一致工作的项目。我已经想出了解决我问题的办法,但我想听听其他的观点,因为我觉得我漏掉了一些东西。

因此,基本上,我有一个访客和一个地点类别, 每一个都是初始化的, 并存储在一个变量中, 例如 美元和美元位置; 两者都拥有访问者独有的数据 和页面呼叫中引用的位置。

我希望/ 需要做的基本上是建一座桥, 这样我就可以调用一种方法来影响这两个对象。 例如, 我想能够调用$Bridge- gt; favorite () ; 并且除了增加该位置所爱的人的数量之外, 还会在访问者简介中添加最喜爱的引用 。

我现在所做的基本上就是一座桥梁:

$bridge = new Bridge($locationclass, $visitorclass);
$bridge->favorite();

所以基本上它需要另一个我拥有的班级, 桥,用方法最喜欢的, 然后使用已经设置的这两个班级。我认为这是一个不错的解决方案, 但我觉得我错过了一个更好的解决方案。

所有的投入都会非常有帮助 谢谢你抽出时间!

最佳回答

如果您需要做这种事情, 这意味着您的应用程序没有很好的设计。

To answer your example, what you have to do is adding the location to the visitor s favorite places, only.
And if you want to count how many times were a location added to favorites, count how many visitors have it in their favorites. Don t store two times the same information.

问题回答
class Bridge{

private $classes;

function  construct($classes)
{
 $this->classes  = $classes;
}

function _call($name,$params)
{
  foreach($classes as $class)
  {
    if(method_exists($class,$name)){
     return call_user_method_array($name,$class,$params);
    }
  }else{
   ...
  }
}




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签