php简单模拟后台接口创建类

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/12/19
 * Time: 11:16
 */
class Goods{
    private $id,$name,$price,$image;

    function getId(){
       return $this -> id;
    }
    function getName(){
        return $this -> name;
    }
    function getPrice(){
        return $this -> price;
    }
    function getImage(){
        return $this -> image;
    }
    function setId($id){
        $this -> id = $id;
    }
    function setName($name){
        $this -> name = $name;
    }
    function setPrice($price){
        $this -> price = $price;
    }
    function setImage($image){
        $this -> image = $image;
    }
    function __construct($id,$name,$price,$image){
        $this -> id = $id;
        $this -> name = $name;
        $this -> price = $price;
        $this ->image = $image;
    }
    function toArray(){
        $a = array(
            "id" => $this -> id,
            "name" => $this -> name,
            "price" => $this -> price,
            "image" => $this -> image
        );
        return $a;
    }

}
$g1 = new Goods("1","外星人电脑","¥2345","images/1.jpg");
$g2 = new Goods("2","唇彩","¥45","images/2.jpg");
$g3 = new Goods("3","酷睿","¥2345","images/3.jpg");
$g4 = new Goods("4","面膜","¥45","images/4.jpg");
$g5 = new Goods("5","韩束","¥45","images/5.jpg");
$g6 = new Goods("6","手机","¥45","images/6.jpg");

$goodlist = array($g1 -> toArray(),$g2 -> toArray(),$g3 -> toArray(),$g4 -> toArray(),$g5 -> toArray(),$g6 -> toArray());
echo json_encode($goodlist);
 
 
 
 
 
 
 
 
 
 
 
 
 
 

猜你喜欢

转载自blog.csdn.net/yaya1286249672/article/details/53981759