|
22-07-2009, 08:57
|
|
|
חבר מתאריך: 25.08.03
הודעות: 9,114
|
|
מחלקות לא נהיו טרנד בזמן האחרון, מה שכן יתכן והשתנה, הוא שלאחרונה אתה, באופן אישי! יותר נתקלת בדיבורים עליה.
אמנם נתתי דוגמה בשפה אחרת ויעוד שונה, אך זה לא פחות מתאים לשפת תסריט בצד השרת.
לדוגמה? מחלקה לטיפול בתמונות.
במקום ליצור אוסף פונקציות עבור טיפול בתמונה, כגון, שינוי גודל, ריפוד, שמירה, הוספת מסגרת, שינוי צבע רקע, מסננים, בהירות, ניגודיות ועוד. ובנוסף לזאת להגדיר בכל פונקציה בנפרד את הטיפול בשגיאות, תמיכה והמרת תבנית הקובץ ועוד.
אתה יכול ליצור מחלקה עם המשתנים והשיטות הנדרשות, ובקטע המעשי, להשתמש בשורת קוד אחת או שתיים לרוב המקרים ומקסימום עוד שורת קוד אחת או שתיים, עבור השימוש בפועל (יצירת מופע וביצוע הפעולה).
קוד לדוגמה:
קוד PHP:
<?php
//מחלקה לדוגמה
class IMG
{
const IMG_NO_ERROR = 0;
const IMG_SRC_IS_BIGER = 1;
//...
protected $fileName = '';
protected $gdObject;
protected $error = array();
protected $defaultWidth = 300;
protected $defaultHeight = 250;
protected $defultImgFormat = 'jpg';
protected $defultImgQuality = 80;
/**
* 0 = not supported
* 1 = full supported
* -2 = support only in input
* -3 = support only for output
*
* MIME Reference - http://www.w3schools.com/media/media_mimeref.asp
**/
protected $supportedMimeTypes = array(
# images
'image/jpeg' => 1,
'image/gif' => 1,
'image/png' => 1,
'image/xpm' => -2,
'image/svg+xml' => 0,
'image/psd' => 0,
//...
# archives
'application/x-rar-compressed' => 0,
//...
# adobe
'image/pdf' => 0,
//...
# video
'video/mpeg' => 0, # mp2, mpa, mpe, mpeg, mpg, mpv2
//...
'text/plain' => 0
);
public function __construct($filePath='', $method='', $params=array())
{
//...
}
public function save($dstFilePath, $format='')
{
if(!$format)
$format = $this->defultImgFormat;
switch($format)
{
//...
}
}
/**
*
**/
public function resize($newX, $newY=null, $enlarge=true, $pad=false)
{
//...
}
public function resizeAndSave($dstFilePath, $newX, $newY=null, $enlarge=true)
{
//...
}
public function crop($newWidth, $newHeight, $leftPoint, $topPoint)
{
//...
}
public function cropAndSave($dstFilePath, $newWidth, $newHeight, $leftPoint, $topPoint)
{
if(!$this->crop($newWidth, $newHeight, $leftPoint, $topPoint))
return false;
return $this->save($dstFilePath);
}
public function pad($width, $height, $backgroundColor, $widthPosition='center', $heightPosition='middle')
{
//...
}
public function addFrame($color, $topWidth=1, $rightWidth=false, $bottomWidth=false, $leftWidth=false)
{
//...
}
public function brighten($value)
{
//...
}
public function genColor($red, $green=0, $blue=0)
{
return $red*256*256 + $green*256 + $blue;
}
public function getWidth()
{
return imagesx($this->gdObject);
}
public function getHeight()
{
return imagesy($this->gdObject);
}
public function getGDimage()
{
return $this->gdObject;
}
public function export($format='',$quality=false)
{
if(!$format)
$format = $this->defultImgFormat;
if($quality === false)
$quality = $this->defultImgQuality;
switch($format)
{
//...
}
}
public function setQuality($quality)
{
return $this->defultImgQuality = $quality;
}
public function getLastError()
{
//...
}
public function getLastErrorCode()
{
//...
}
public function getErrorMsg($errorCode)
{
switch($errorCode)
{
case self::IMG_NO_ERROR:
return 'No Error found';
case self::IMG_SRC_IS_BIGER:
return 'The source image is bigger than the required dimensions, see more details in IMG->resize() method documentation';
//...
Default:
return 'Unknown';
}
}
public function isFormatSupported($mimeType)
{
return ($this->supportedMimeTypes[$mimeType] == 1);
}
/**
* ...
*
* 0 = not supported
* 1 = full supported
* -2 = support only in input
* -3 = support only for output
**/
public function formatSupportedType($mimeType)
{
return (int)$this->supportedMimeTypes[$mimeType];
}
public function __destruct()
{
imagedestroy($this->gdObject);
unset($this->gdObject);
unset($this->error);
}
}
//דוגמת שימוש
$img = new IMG($image_file_name);
$img->setQuality(80);
if(!$img->resizeAndSave($image_file_name, '400', '300', false))
echo 'Error occure while trying to resize and save the image';
//דוגמה נוספת
if(!$img->crop($dir.$new_file_name, $dimensions[0], $dimensions[1], false))
echo 'Error occure while trying to crop the image';
בצורת תכנות כזאת, אין לך צורך להמציא את הגלגל מחדש. אתה יכול לנייד את קהוד לכל מקום, לשתף אותו עם אחרים, לשפר, לתקן באג ולעדכן את כל העתקים הישנים של המחלקה (להבדיל מלעבור על קבצים רבים, לחפש את הפונקציות הקשורות והשונות ולתקן בכל אחת בנפרד).
במידה זהה, השימוש הוא עבור כמעט כל דבר אחר בשפה.
מחלקה לטיפול במסדי נתונים. אתה יכול לדוגמה לעבור להשתמש במנוע מסד נתונים אחר או אף במסד נתונים שונה, וכל זה מבלי לגעת בקוד של המערכת עצמה (שהיא מגישה את השאילות ומקבלת תשובות מהמחלקה האחראית).
|
|