Parseerror: syntaxerror, unexpectedstatic (T_STATIC ),
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 'state' (T_STATIC), expecting identifier (T_STRING) in D: \ Apache24 \ htdocs \ PHP_OBJECT \ 4 \ 4.1.2.php on line 46
Why is an error reported when writing a function static getInstance static variable like this? Static function to output data?
Reply to discussion (solution)
Function static write reversed, should be
Static function
Function static write reversed, should be
Static function
Is this written in the php document?
Maybe so. this is common sense!
Static function getInstance ($ id, PDO $ pdo ){
Static: modifies the function. it means that the function named getInstance is static.
If you write function static getInstance ($ id, PDO $ pdo) {
Not to mention that the static location is incorrect.
The function should be followed by the function name. is it static getInstance?
The function name cannot be split into two sections. this is not a syntax.
Maybe so. this is common sense!
Static function getInstance ($ id, PDO $ pdo ){
Static: modifies the function. it means that the function named getInstance is static.
If you write function static getInstance ($ id, PDO $ pdo) {
Not to mention that the static location is incorrect.
The function should be followed by the function name. is it static getInstance?
The function name cannot be split into two sections. this is not a syntax.
Class StaticExample {
Public static $ aNum = 0;
Private static function sayHello () {// can be sorted by both static and static private ??
Self: $ aNum ++;
Print 'Hello ('. self: $ aNum .')';
}
Function getSayHello (){
Self: sayHello ();
}
}
StaticExample: $ aNum;
$ StaticExample = new StaticExample ();
$ StaticExample-> getSayHello ();
However, no error is reported. The php version is 5.4.32.
Incorrect. thank you. Check out.
Change function static getInstance to static function getInstance.
Static is a modifier, which needs to be written before the modified variables or methods according to the syntax rules.