Blogger Information
Blog 54
fans 4
comment 1
visits 54801
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
静态方法的使用
神仙不在的博客
Original
918 people have browsed it

<?php
class Db
{
   public $product;
//    静态属性
   public static $price;
//    初始化,右键generate->construct
   public function __construct($product,$price)
   {
       $this->product = $product;
       self::$price = $price;
   }
   public function getInfo()
   {
       return $this->product.'价格是:'.self::$price;
   }
//    这是个技巧,注入的方法,把$product传进去,return得到
   public static function getInfo2($product)
   {
       return $product.'价格是:'.self::$price;
   }
}
$db = new Db('衣服',300);
echo $db->getInfo(),'<hr>';
echo $db->product,'<hr>';
echo Db::$price,'<hr>';
$product = $db->product;
//居然实例化的对象可以这样调用静态方法
echo $db->getInfo2($product);
//类调用静态方法
echo Db::getInfo2('衣服');

静态方法可以被类调用,也可以被实例化的对象调用

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post