Php 为什么在课堂上重复内容?
我编写了一些类来准备信息,并将其发送给父类以获取teplate: 说明: 创建对象: 查看socialfeed/模板:Php 为什么在课堂上重复内容?,php,class,oop,Php,Class,Oop,我编写了一些类来准备信息,并将其发送给父类以获取teplate: 说明: 创建对象: 查看socialfeed/模板: 这不难解释,因为这里有连接$this->template.=$video->getTemplate;看到了吗?=?是的,它是正确的。所有结果都是一个字符串。我尝试了var_dump$this->data,看到$media exix只在一个数组中,这是一个很好的方法,但不理解为什么它会显示在任何模板中,那么您希望看到什么呢?如果你的$this->模板已经有一些字符串,并且你用另一
这不难解释,因为这里有连接$this->template.=$video->getTemplate;看到了吗?=?是的,它是正确的。所有结果都是一个字符串。我尝试了var_dump$this->data,看到$media exix只在一个数组中,这是一个很好的方法,但不理解为什么它会显示在任何模板中,那么您希望看到什么呢?如果你的$this->模板已经有一些字符串,并且你用另一个字符串连接了它-其中有两个字符串。是的,例如有两篇文章,第一个视频和文章。视频的获取字符串模板和文章中的获取字符串。在将它们组合为commnon字符串之后,在一个字符串中获得两个post。但是我找不到一个错误,为什么从一个对象到另一个对象的数据是循环中的开关?
// Parent class
class FeedTemplate {
public $data = array();
function __construct($data = array()){}
function getTemplate(){
return $this->CI->load->view('socialfeed/template', $this->data, true);
}
}
class ArticleTemplate extends FeedTemplate {
}
class VideoTemplate extends FeedTemplate {
function __construct($data = array()){
parent::__construct($data);
}
function getTemplate(){
$this->data['media'] = $this->CI->load->view('video/video_single', $this->data, true);
parent::getTemplate();
}
}
...
case 'article':
$obj = new ArticleTemplate($value);
$this->template.= $obj->getTemplate();
break;
case 'video':
$video = new VideoTemplate($value);
$this->template.= $video->getTemplate();
break;
...
<div class="text"><?=$media?><?=$content['text']?></div>
class FeedTemplate {
public $data = array();
protected $inputdata = array();
public $width_content = 500;
private $category;
protected $CI;
function __construct($data = array()){
$this->inputdata = $data;
$this->CI =& get_instance();
$this->data['user'] = $this->getUserInfo($this->inputdata->user);
/* Load Libraries */
$this->CI->load->library('translate');
}
protected function getCategory($url){
if(!empty($this->inputdata->categoryPrivate)){
return ', <a target="_blank" href="'.$url.'">'.$this->inputdata->categoryPrivate.'</a>';
} else if(!empty($this->inputdata->categoryPublic)) {
$this->CI->load->library('listdata');
$this->category['id'] = $this->CI->listdata->specializationList[$this->inputdata->categoryPublic]['idSpecialization'];
$this->category['name'] = $this->CI->listdata->specializationList[$this->inputdata->categoryPublic]['SpecializationName'];
return ', <a target="_blank" href="'.$url.$this->category['id'].'">'.$this->category['name'].'</a>';
}
}
protected function getUserInfo($data){
$user = new InformationUser($data);
return $user->getUser();
}
function getHash($id, $type){
return $this->hash = HashDataView(array('id' => $id, 'type' => $type));
}
function getTemplate(){
return $this->CI->load->view('socialfeed/template_post', $this->data, true);
}
}
class ArticleTemplate extends FeedTemplate {
public $category;
public $photo;
public $width = 495;
public $height = '';
function __construct($data = array()){
parent::__construct($data);
$this->data['content']['title'] = $data->ArticleName;
$this->data['content']['time'] = $data->ArticleTime;
$this->data['content']['text'] = $data->ArticleDescription;
$this->data['content']['url_post'] = '/articles/'.$data->id;
$this->data['content']['category'] = 'Articles';
$this->data['content']['category_url'] = 'articles';
$this->data['content']['id'] = $data->id;
}
function getImage(){
$number = count($this->photo);
if ($number > 0){
$images = '';
$this->getResizeImage($number);
foreach($this->photo as $key => $value){
$img['title'] = $key;
$img['src'] = $value;
$img['width'] = $this->width;
$img['height'] = $this->height;
$images.= $this->getImageHTML($img);
}
return '<div class="photo">'.$images.'</div>';
}
}
function getImageData($src, $type){
switch($type){
case 'width':
return imagesx($src);
case 'height':
return imagesy($src);
}
}
function getResizeImage($number){
switch($number){
case 2:
$this->width = $this->width_content / $number - 5;
$this->height = 160;
break;
case 3:
foreach($this->photo as $key => $value){
$this->photo[$key] = $this->getImageData($value['src'], 'height');
}
asort($this->photo);
break;
case 4:
$this->width = $this->width_content / 2;
break;
}
}
function getImageHTML($data = array()){
return '<div style="width:'.$data['width'].'px; height:'.$data['height'].'px">
<a href="'.$data['src'].'" class="fancybox"><img alt="'.$data['title'].'" title="'.$data['title'].'" width="'.$data['width'].'" src="'.$data['src'].'"></a></div>';
}
}
class VideoTemplate extends FeedTemplate {
public $width;
public $height;
public $thumb;
public $code;
public $album;
public $hash;
private $id;
private $category;
private $album_url;
function __construct($data = array()){
parent::__construct($data);
$this->data['content']['title'] = $data->usersalbumsvideoTitle;
$this->data['content']['time'] = $data->usersalbumsvideoTime;
$this->data['content']['id'] = $data->id;
$this->thumb = $data->usersalbumsvideoThumb;
$this->code = $data->usersalbumsvideoCode;
$this->album_url = $this->getAlbumLink($data);
$this->id = $data->id;
$this->height = 270;
$this->width = 400;
$this->getVideo();
}
private function getAlbumLink($data){
return '/wm'.$data->user['id'].'/video/albums/'.$data->albumID;
}
function getCategory(){
$this->data['content']['category'] = 'Video';
$this->data['content']['category_url'] = 'video';
return parent::getCategory($this->album_url);
}
function getVideo(){
$this->data['video']['id'] = $this->id;
$this->data['video']['thumb'] = $this->thumb;
$this->data['video']['code'] = $this->code;
$this->data['video']['height'] = $this->height;
$this->data['video']['width'] = $this->width;
$this->data['content']['url_post'] = $this->album_url;
$this->data['content']['sub_category'] = $this->getCategory();
$this->data['video']['hash'] = parent::getHash($this->id, 6);
}
function getTemplate(){
$this->data['media'] = $this->CI->load->view('video/video_single', $this->data, true);
parent::getTemplate();
}
}
Class InitTemplate {
public $template;
function __construct($data = array()){
$this->init($data);
}
private function decodeJSON($value){
return json_decode($value);
}
private function init($data){
foreach($data['data'] as $value => $key){
$value = $this->decodeJSON($value);
$value->user = current($data['user']);
switch($value->type){
case 'article':
$obj = new ArticleTemplate($value);
$this->template.= $obj->getTemplate();
break;
case 'video':
$video = new VideoTemplate($value);
$this->template.= $video->getTemplate();
break;
case 'link':
$link = new LinkTemplate($value);
$this->template.= $link->getTemplate();
break;
case 'comment':
break;
case 'new':
$obj = new NewTemplate($value);
$this->template.= $obj->getTemplate();
break;
case 'vacance':
break;
case 'stock':
$obj = new StockTemplate($value);
$this->template.= $obj->getTemplate();
break;
case 'photo':
break;
case 'question':
$obj = new QuestionTemplate($value);
$this->template.= $obj->getTemplate();
break;
case 'answer':
break;
case 'albumphoto':
break;
case 'videoalbum':
break;
}
}
}
public function getTemplateVar(){
return $this->template;
}
}