Home > Backend Development > PHP Tutorial > A must-read for newbies like PHP basics

A must-read for newbies like PHP basics

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-28 08:26:06
Original
1006 people have browsed it
header("content-type:text/html;charset=utf-8");//解析中文编码,必不可少
class car{ // 定义car类
    private $name; // 定义name属性
    function __construct($name){ // 构造函数
        $this->name=$name;
    }
    public function getName(){ // 定义方法
        return $this->name;
    }
    public function getCarPrice($price)//定义函数有返回值并传参的函数
    {
        return $price;
    }
    //创建static 静态方法
    static function mystatic($a,$b){
        $c= $a+$b;
        echo "我是static方法";
        echo  "$c";
    }

    function __destruct(){ // 析构函数无须调用
        echo "car对象被销毁!";
    }
}
$benz = new car("benz"); // 实例化car类:new+类名称就成为了一个对象,对象名为$byd
$bmw = new car("bmw"); // 实例化car类
echo $benz->getName().'<br />'; // 通过对象$benz调用car类的getName方法
echo $bmw->getName().'<br />'; // 通过对象$bmw调用car类的getNmae方法
echo $benz->getCarPrice("$66000")."<br/>";//通过对象$benz调用car类的getCarPrice方法.返回price
//静态方法(类方法)调用,直接通过类名来调用
site ::mystatic(3,5);//"::"为调用符号相当于java中方法调用的"."
echo "<br/>";

$benz = null; // 释放对象资源
$bmw = null; // 释放对象资源
Copy after login

这就是今天和大家分享的学习笔记。希望有所帮助。

以上就介绍了 菜鸟必看php基础之类对,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template