>  기사  >  php教程  >  PHP 구성 요소 프로그래밍 기술

PHP 구성 요소 프로그래밍 기술

黄舟
黄舟원래의
2016-12-14 13:09:281062검색

그러나 UI 편의성 측면에서는 다소 부족합니다. UI를 디자인할 때 PHP뿐만 아니라 모든 웹 프로그래밍 언어가 비슷한 문제를 가지고 있습니다. 하나의 파일에 HTML 코드가 많이 반복되어 있기 때문입니다. 기술적 내용은 없지만 시간이 많이 걸리고 노동 집약적입니다. 그래서 이전에 수행했던 PHP 프로젝트의 UI 부분을 요약하고 이를 작은 구성 요소(델파이의 구성 요소와 마찬가지로)로 캡슐화하고 인터페이스에서 통일된 스타일로 표시할 수 있기를 바랍니다. 나중에 다시 사용할 수 있습니다. 이 매듭 구성 요소에 대해 여러 CSS 파일을 작성하여 "스키닝" 기능을 제공합니다.

모든 구성 요소는 AbatractComponent 클래스에서 상속되며 해당 toString() 및 render() 메서드를 구현합니다. AbatractComponent에는 세 가지 주요 하위 클래스가 있습니다. 하나는 Panel, PopPanel 및 GroupPanel과 같은 클래스를 파생하는 컨테이너 클래스입니다. 두 번째는 Button, LinkButton과 같은 모든 시각적 컨트롤 클래스의 상위 클래스인 Control입니다. 등의 클래스이고, 세 번째 클래스는 목록과 이름-값 쌍을 사용하여 UI를 구현하는 목록 클래스 List입니다.

AbstractComponent부분대码:
/** 
* 구성 요소 라이브러리 

* @author Chris Mao 
* @package 구성 요소 
* @description 모든 구성 요소는 클래스에서 확장되어야 하며
* toString의 두 메서드를 모두 재정의해야 합니다. . 
* @copyright Copyright (c) 2009 JueRui Soft Studio 

**/ 
class AbstractComponent { 

/* 
* @var _style 구성 요소 스타일의 배열

* @access protected 

*/ 
protected $_style = array(); 
/* 
* @var _attributes 구성 요소 속성의 문자열 

* @access protected 

*/ 
protected $_attributes = array(); 

/** 
* 생성자 함수 

* @access public 

*/ 
공용 함수 __construct($options = null, $style = null) { 
if (!is_null($options) && (gettype($options) != "배열")) { 
throw new Exception("옵션은 배열이어야 합니다!!"); 

if (!empty($options) && is_array($options)) { 
if (array_key_exists("style", $options)) { 
if (is_array($options[" style"])) { 
$this->_style = array_merge($this->_style, $options["style"]); 

unset($options["style"]); 

$this->_attributes = array_merge($this->_attributes, $options); 

if (!empty($style) && is_array($style)) { 
$this->_style = array_merge($this->_style, $style); 



/** 
* 구성요소 속성 설정 

* @access protected 

* @param $name 속성 이름 
* @param $value 속성 값, 옵션 

* @return AbstractComponent 
*/ 
보호 함수 setAttr($name, $value) { 
if (array_key_exists($name, $this->) ;_attributes)) { 
unset($this->_attributes[$name]); 

$this->_attributes[$name] = $value; 
$this를 반환합니다. 


/** 
* 구성요소 속성 값 가져오기 

* @access protected 

* @param $name 속성 이름 

* @return 문자열 
*/ 
보호 함수 getAttr($name) { 
return array_key_exists($name, $this->_attributes) ? $this->_attributes[$name] : null; 


/** 
* 구성요소 스타일 설정 

* @access protected 

* @param $name 스타일 이름 
* @param $value 스타일 값, 옵션 

* @return AbstractComponent 
*/ 
보호 함수 setStyle($name, $value) { 
if (array_key_exists($name, $this->_style)) { 
unset($this->_style[$name]); 

$this->_style[$name] = $value; 
$this를 반환합니다. 


/** 
* 구성요소 스타일 값 가져오기 

* @access protected 

* @param $name 속성 이름 

* @return 문자열 
*/ 
보호 함수 getStyle($name) { 
return array_key_exists($name, $this->_style) ? $this->_style[$name] : null; 


/** 
* 구성 요소의 모든 속성을 name = "value"와 같은 문자열로 변환 

* @access protected 

* @return string 
*/ 
보호 함수 attributeToString() { 
//$s = array_reduce(; 
$s = ""; 
foreach($this->_attributes as $key => $value) { 
$s .= " $key="$value" "; 

$s 반환;
}

/** 
* 구성 요소 스타일을 style = "....."와 같은 문자열로 변환 

* @access protected 

* @return string 
*/ 
보호된 함수 styleToString() { 
if (empty($this->_style)) return ""; 
$s = "" ; 
foreach($this->_style as $key => $value) { 
$s .= " $key: $value; "; 

$s = " 스타일 ="$s" "; 
return $s; 


/** 
* 구성요소 속성 설정 또는 가져오기 

* @access public 

* @param $name 속성 이름 
* @param $value 속성 값, 옵션 

* @return 문자열 || AbstractComponent
*/ 
공개 함수 attr() { 
$name = func_get_arg(0 ); if (func_num_args() == 1) { 
return $this->getAttr($name); 

else if (func_num_args() == 2) { 
$value = func_get_arg(1); 
return $this->setAttr($name, $value); 



/** 
* 구성요소 스타일 설정 또는 가져오기 

* @access public 

* @param $name 스타일 이름 
* @param $value 스타일 값, 옵션 

* @return 문자열 || AbstractComponent
*/ 
공개 함수 스타일() { 
$name = func_get_arg(0); 
if (func_num_args() == 1) { 
return $this->getStyle($name); 

else if (func_num_args() == 2) { 
$value = func_get_arg(1); 
return $this->setStyle($name, $value); 



/** 
* HTML 문자열 반환 

* @access public 

* @return 문자열 
**/ 
public function toString() { 
thorw New AbstractException("하위 클래스는 이 메서드를 재정의해야 합니다!!"); 


/** 
* 구성요소 렌더링 

* @access public 

* @return void 
**/ 
공개 함수 render() { 
echo $this->toString(); 

}

更多敳关内容请关注PHP中文网(www.php.cn)!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.