类需要一个初始化函数,函数名与类名相同,初始化类的时候这个构造函数就开始运行,构造函数可以使用参数,也就是调用类的时候需要的参数。
下面是FPDF类的构造函数,带了三个参数,分别是页面方向(默认为纵向)、使用的单位(默认为 mm)、页面样式(默认为 A4)。
PHP代码
- function FPDF($orientation='P', $unit='mm', $format='A4')
- {
- // 调用私有方法 _dochecks() 进行检查
- $this->_dochecks();
- // 属性初始化
- $this->page = 0;
- $this->n = 2;
- $this->buffer = '';
- $this->pages = array();
- $this->PageSizes = array();
- $this->state = 0;
- $this->fonts = array();
- $this->FontFiles = array();
- $this->diffs = array();
- $this->images = array();
- $this->links = array();
- $this->InHeader = false;
- $this->InFooter = false;
- $this->lasth = 0;
- $this->FontFamily = '';
- $this->FontStyle = '';
- $this->FontSizePt = 12;
- $this->underline = false;
- $this->DrawColor = '0 G';
- $this->FillColor = '0 g';
- $this->TextColor = '0 g';
- $this->ColorFlag = false;
- $this->ws=0;
- // 标准字体
- $this->CoreFonts = array('courier'=>'Courier', 'courierB'=>'Courier-Bold',
- 'courierI'=>'Courier-Oblique', 'courierBI'=>'Courier-BoldOblique',
- 'helvetica'=>'Helvetica', 'helveticaB'=>'Helvetica-Bold',
- 'helveticaI'=>'Helvetica-Oblique','helveticaBI'=>'Helvetica-BoldOblique',
- 'times'=>'Times-Roman', 'timesB'=>'Times-Bold', 'timesI'=>'Times-Italic',
- 'timesBI'=>'Times-BoldItalic', 'symbol'=>'Symbol',
- 'zapfdingbats'=>'ZapfDingbats');
- // 比例因子
- if($unit == 'pt')
- $this->k = 1;
- elseif($unit == 'mm')
- $this->k = 72/25.4;
- elseif($unit == 'cm')
- $this->k = 72/2.54;
- elseif($unit == 'in')
- $this->k = 72;
- else
- $this->Error('Incorrect unit: '.$unit);
- // 页面式样,调用私有方法 _getpageformat()
- $this->PageFormats = array('a3'=>array(841.89,1190.55),
- 'a4'=>array(595.28,841.89), 'a5'=>array(420.94,595.28),
- 'letter'=>array(612,792), 'legal'=>array(612,1008));
- if(is_string($format))
- $format = $this->_getpageformat($format);
- $this->DefPageFormat = $format;
- $this->CurPageFormat = $format;
- // 页面方向
- $orientation = strtolower($orientation);
- if($orientation == 'p' || $orientation == 'portrait')
- {
- $this->DefOrientation = 'P';
- $this->w = $this->DefPageFormat[0];
- $this->h = $this->DefPageFormat[1];
- }
- elseif($orientation == 'l' || $orientation == 'landscape')
- {
- $this->DefOrientation = 'L';
- $this->w = $this->DefPageFormat[1];
- $this->h = $this->DefPageFormat[0];
- }
- else
- $this->Error('Incorrect orientation: '.$orientation);
- $this->CurOrientation = $this->DefOrientation;
- $this->wPt = $this->w * $this->k;
- $this->hPt = $this->h * $this->k;
- // 页边距(默认为 1 cm),调用SetMargins()
- $margin = 28.35 / $this->k;
- $this->SetMargins($margin, $margin);
- // 内部单元边距(默认为 1 mm)
- $this->cMargin = $margin / 10;
- // 线的粗细(默认为 0.2 mm)
- $this->LineWidth = .567 / $this->k;
- // 换页边距,即页面下边距(默认为 2 cm),调用SetAutoPageBreak()
- $this->SetAutoPageBreak(true, 2*$margin);
- // 显示模式,默认为窗口最大宽度,连续分页,调用SetDisplayMode()
- $this->SetDisplayMode('fullwidth');
- // 使用压缩,调用SetCompression()
- $this->SetCompression(true);
- // 设置PDF文件版本号,默认为1.3,即4.x以上Adobe Reader可读
- $this->PDFVersion = '1.3';
- }
构造函数中调用了2个私有方法 _dochecks() 和 _getpageformat(),还有5个公共方法 Error()、SetMargins()、SetAutoPageBreak()、SetDisplayMode() 和 SetCompression(),下一篇逐个分析它们。

