1、寫一段代碼,實現PHP內部的通知機制,如當一個類的屬性發生變化時,另外一個類就可以收到通知。
<?php
/**
* 觀察者模式
* @author: Mac
* @date: 2012/02/22
*/
class Paper{ /* 主題 */
private $_observers = array();
public function register($sub){ /* 註冊觀察者 */
$this->_observers[] = $sub;
}
public function trigger(){ /* 外部統一訪問 */
if(!empty($this->_observers)){
foreach($this->_observers as $observer){
$observer->update();
}
}
}
}
/**
* 觀察者要實現的接口
*/
interface Observerable{
public function update();
}
class Subscriber implements Observerable{
public function update(){
echo "Callback\n";
}
}
下面是測試代碼
/* 測試 */
$paper = new Paper();
$paper->register(new Subscriber());
//$paper->register(new Subscriber1());
//$paper->register(new Subscriber2());
$paper->trigger();
2、 以下代碼會輸出結果嗎?如果能請說明原因,如果不能請嘗試修改使其能輸出結果
class CateData {
private $_data=array();
public function __construct($data){
$this->_data=$data;
}
}
$data = array(
'a'=>'分類1',
'b'=>'分類2',
'c'=>'分類3',
'd'=>'分類4',
);
$cate=new CateData($data);
foreach($cate as $key => $val){
echo "$key : ". $val;
}
不會
可修改爲
$cate=new CateData($data);
foreach($cate as $key => $val){
echo "$key : ". $val;
3、看代碼回答問題
$a = 1;
function func($a){
echo $a;
$a = 100;
}
func($a);
echo $a;
class A{
public $a = 10;
}
$obj = new A();
func($obj->a);
echo $obj->a;
請說出以上代碼輸出的結果,詳細說明兩次調用的差異
111010;
4、如何解決或降低緩存服務的單點故障導致數據庫的流量井噴
解決單點故障的最好方法就是做數據庫服務器集羣,同時讓數據庫做好主從備份保證數據完整性,以及適當數據庫冗餘,當一臺緩存服務出現故障,從服務器通過檢測心跳可快速提供服務,保證服務的不間斷;再加上MySQL Proxy實現負載均衡、讀寫分離等功能,在使用主從複製的基礎上,再使用垂直切分及水平切分;或者不使用主從複製,完全使用垂直切分加上水平切分再加上類似Memcached的內存緩存系統可以解決問題。
5、請用PHP實現一個雙向鏈表,並提供基本的增刪改查功能
<?php
class link
{
public $preLink = NULL; //前指針
public $number; //排名
public $name; //名字
public $nextLink = NULL; //後指針
//構造函數,初始化值
public function __construct($number= '', $name = '')
{
$this->number = $number;
$this->name = $name;
}
//增加數據
static public function addDom($head, $data)
{
$obj = $head;
$isExist = FALSE;
if(NULL == $obj->nextLink)
{
$obj->nextLink = $data;
$data->preLink = $head;
}
//找到添加的位置
while(NULL != $obj->nextLink)
{
if($obj->nextLink->number > $data->number)
{
break;
}
else if($obj->nextLink->number == $data->number)
{
$isExist = TRUE;
echo "<br>不能添加相同的編號";
}
$obj = $obj->nextLink;
}
if(!$isExist)
{
if(NULL !=$obj->nextLink)
{
$data->nextLink =$obj->nextLink;
}
$data->preLink = $obj;
if(NULL !=$obj->nextLink)
{
$data->nextLink->preLink = $data;
}
$obj->nextLink = $data;
}
}
//輸出鏈表數據
static public function showDom($head)
{
$obj = $head;
while(NULL != $obj->nextLink)
{
echo "<br>編號:".$obj->nextLink->number."名字:".$obj->nextLink->name;
$obj = $obj->nextLink;
}
}
static public function delDom($head, $num)
{
$obj = $head;
$isFind = FALSE;
while(NULL != $obj)
{
if($obj->number == $num)
{
$isFind = TRUE;
break;
}
$obj = $obj->nextLink;
}
if($isFind)
{
if(NULL !=$obj->nextLink)
{
$obj->preLink->nextLink = $obj->nextLink;
echo '<br/>刪除的號碼是'.$obj->number;
}
else
{
echo "<br>沒有找到目標";
}
}
}
static public function updateDom($head, $num, $name)
{
$obj = $head;
while(NULL != $obj)
{
if($obj->number == $num)
{
$obj->name = $name;
echo "<br/>改變號碼{$obj->number}的結果:".$obj->name;
break;
}
$obj = $obj->nextLink;
}
}
}
$head = new link();
$one = new link(1,'oooooooo');
$two = new link(2,'wwwwwwww');
$three = new link(3,'eeeeeeee');
link::addDom($head,$one);
link::addDom($head,$two);
link::addDom($head,$three);
link::showDom($head);
link::delDom($head, 2);
link::showDom($head);
link::updateDom($head,3, 'kkkkkk');
link::showDom($head);
6、設計一個數據庫表,實現評論樓蓋樓效果,並寫出sql語句
a、寫出建表語句
create table comment
(
`id` int(10) unsigned not null auto_increment primary key,
// 通過把id逗號連接確定父級關係 1,3,5
`path` varchar(100) not null,
'deepth' tinyint(3) not null,
`comment_text` text not null,
`time` int(10) not null,
) engine=innodb defalut charset=utf8;
b、查詢一個評論的所有子評論
select * from commet where path regexp (該父評論的path + "," + 父評論的id + ",");
c、寫出每次新增和刪除的sql語句
insert into comment (父評論的path + 父評論的 id, 父評論的deepth + 1 , '評論內容' , now());
delete from coment where id = 要刪除的id
d、寫出計算層級深度的sql語句
select deepth from comment where id = 要計算的id
7、美圖APP有上億用戶使用,爲了深刻挖掘用戶需求,需要設計一個日誌收集系統,用於收集用戶日常使用情況,請用你知道的知識設計一套這樣的日誌收集系統。
假設,用戶每次操作、點擊美圖APP都將發起請求。
請給出系統架構設計圖,數據庫存儲方式,需要的機器量,以及能承載的QPS,儘可能詳細的描述此係統中的難點、解決方案。
參考我的segmentfault問題:https://segmentfault.com/q/1010000013834130