title = $title; $this->producermainname = $mainname; $this->producerfirstname = $firstname; $this->price = $price; } function setdiscount($num){ $this->discount = $num; } function getdiscount(){ return $this->discount; } function gettitle(){ return $this->title; } function getproducerfirstname(){ return $this->producerfirstname; } function getproducermainname(){ return $this->producermainname; } function getid(){ return $this->id; } function setid($id){ $this->id = $id; } function static getinstance($id,pdo $pdo){ $stmt = $pdo->prepare(select * from products_4 where id =?); $result = $stmt->execute(array($id)); $row = $stmt->fetch(); if(empty($row)){ return null; } if($row['type']=='book'){ $product = new bookproduct( $row['title'],$row['firstname'],$row['mainname'],$row['price'],$row['numpages'] ); } elseif($row['type']=='cd') { $product = new cdproduct( $row['title'],$row['firstname'],$row['mainname'],$row['price'],$row['playlength'] ); } else{ $product = new shopproduct( $row['title'],$row['firstname'],$row['mainname'],$row['price'] ); } $product->setid($row['id']); $product->getdiscount($row['discount']); return $product; } function getprice(){ return ({$this->price} - {$this->discount}); } function getproducer(){ return {$this->producerfirstname}. {$this->producermainname}; } function getsummaryline(){ $base = {$this->title} ( {$this->producermainname}; $base .= {$this->producerfirstname} ); return $base; }}class cdproduct extends shopproduct{ private $playlength = 0; function __construct($title,$firstname,$mainname,$price,$playlength){ parent::__construct($title,$firstname,$mainname,$price); $this->playlength = $playlength; } function getsummaryline(){ $base = parent::getsummaryline(); $base .= : playing - time {$this->playlength}; return $base; }}class bookproduct extends shopproduct{ private $numpages = 0 ; function __construct($title,$firstname,$mainname,$price,$numpages){ parent::__construct($title,$firstname,$mainname,$price); $this->numpages = $numpages; } function getnumpages(){ return $this->numpages; } function getsummaryline(){ $base = parent::getsummaryline(); $base .= : page count - {$this->numpages}; return $base; } function getprice(){ return $this->price; }}$dsn = mysql:host=localhost;dbname=test;try{ $pdo = new pdo($dsn,root,root); $pdo->setattribute(pdo::attr_errmode,pdo::errmode_exception); $obj = shopproduct::getinstance(1,$pdo);}catch(pdoexception $e){ echo $e->getmessage();}print_r($obj);
parse error: syntax error, unexpected 'static' (t_static), expecting identifier (t_string) in d:\apache24\htdocs\php_object\4\4.1.2.php on line 46
function static getinstance 静态变量为什么这样写报错呢? static function 这样就可以输出数据?
回复讨论(解决方案) function static 写反了,应为
static function
function static 写反了,应为
static function
这个在php文档上有写吗?
可能有吧,这是常识!
static function getinstance($id,pdo $pdo){
static 是修饰 function 的,是说名为 getinstance 的 function 是静态的
而你写成 function static getinstance($id,pdo $pdo){ 的话
且不说 static 的位置不对
function 后面应该是函数名,难不成就是 static getinstance ?
函数名也不能拆成两段呀,这不合语法
可能有吧,这是常识!
static function getinstance($id,pdo $pdo){
static 是修饰 function 的,是说名为 getinstance 的 function 是静态的
而你写成 function static getinstance($id,pdo $pdo){ 的话
且不说 static 的位置不对
function 后面应该是函数名,难不成就是 static getinstance ?
函数名也不能拆成两段呀,这不合语法
class staticexample{
public static $anum = 0 ;
private static function sayhello(){ // private static 和static private两样排序都可以
self::$anum++;
print 'hello ('.self::$anum.')';
}
function getsayhello(){
self::sayhello();
}
}
staticexample::$anum;
$staticexample = new staticexample();
$staticexample->getsayhello();
但是这样没有报错,php版本是5.4.32的 看错了,谢谢您。结账。
function static getinstance 改为 static function getinstance
static是修饰符,按语法规则需要写在被修饰的变量或方法前面。