Home  >  Article  >  Backend Development  >  Sample code sharing for php initialization object

Sample code sharing for php initialization object

黄舟
黄舟Original
2017-08-17 09:57:052817browse

There are two ways to initialize a class in php. One is to use the function with the same name as the constructor as you said, and the other is to define __construct() (the first two underscores _) as the constructor. These two Except for the different function names, there is no other difference. They are all used to initialize classes. Today I will introduce to you an example of PHP initialization object!

The first step is to download the PHP initialization object class library we need to use in this course: http://www.php.cn/xiazai/leiku/621

Second Step. After the download is completed, find the php class file we need, unzip it to our local directory, and create a new php file!

The third step, after completion, we need to call this class in the new php file and instantiate the class:

<?php
include_once "dingyi1.php";//引入类文件
$pc1 = new mypc(&#39;家用电脑&#39;,&#39;台式机&#39;);//实例化
echo $pc1->vod()."<br>";
//当$pc1=null;的时候是当前实例操作完成后直接释放出对象
$pic2 = new mypc(&#39;公司电脑&#39;,&#39;笔记本&#39;);
echo $pic2->game();
?>

Run this file and get the result as shown below:

Sample code sharing for php initialization object

The above is the detailed content of Sample code sharing for php initialization object. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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