PHP中哪种设计理念更好;将变量传递给函数 myFunct() 还是不传递变量?
<?php
class A{
public $myvar=1;
public function myFunc($myvar){
$this->myvar=$myvar+1;
}
}
$myA=new A();
$myA->myFunc($myA->myvar);
// OR THIS ONE
class A{
public $myvar=1;
public function myFunc(){
$this->myvar=$this->myvar+1;
}
}
$myA=new A();
$myA->myFunc();
?>
这可能是我试图理解的一个更好的例子:
class PhotosBasicClasses{
protected $srcImage;
protected $fileImageTypeFlag;
public function createThumb($srcImage,$fileImageTypeFlag){
$this->srcImage=$srcImage;
$this->fileImageTypeFlag=$fileImageTypeFlag;
$resourceNewImage=$this->imageCreateFromFormat($srcImage,$fileImageTypeFlag); //with or without the parameters is better?!
}
protected function imageCreateFromFormat($srcImage,$fileImageTypeFlag){
switch($fileImageTypeFlag){ //this is my problem: would be better to use the class variable or the internal variable($fileImageTypeFlag or $this->fileImageTypeFlag )
case 'jpeg': return imagecreatefromjpeg($srcImage);break;
case 'png': return imagecreatefrompng($srcImage);break;
case 'gif': return imagecreatefromgif($srcImage);break;
default: return "error source file format";
}
}