报个错Parse error: syntax error, unexpected 'static' (T_STATIC),

php中文网
发布: 2016-06-20 12:52:39
原创
3188人浏览过

<?phpclass ShopProduct{	  private $id = 0;    private $title;    private $producerMainName;    private $producerFirstName;    protected $price;    private $discount = 0;        function __construct($title,$firstName,$mainName,$price){        $this->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 这样就可以输出数据?

挖错网
挖错网

一款支持文本、图片、视频纠错和AIGC检测的内容审核校对平台。

挖错网 28
查看详情 挖错网

回复讨论(解决方案)

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是修饰符,按语法规则需要写在被修饰的变量或方法前面。

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号