關(guān)于PHP中的Class
Jun 13, 2016 am 10:19 AM
作者:深空 來源:超越PHP
以我的觀點(diǎn)來說說PHP中的Class,用于表達(dá)的語言都是非正式的語言,也不能確定是否正確。
建立一個(gè)類很簡(jiǎn)單:
class my_class {}
類到底干什么呢?很多人都說是什么黑匣子,我在這里稱它為一個(gè)獨(dú)立的整體。我們只知道類名,而不知道里面有什么東西。那么,該如何使用這個(gè)類呢?
首先:要知道它里面是否定義了公共的變量--專業(yè)術(shù)語上稱它為“屬性”。
其次:要知道它里面定義了什么函數(shù)--專業(yè)術(shù)語中稱它為“方法”。
我都被這些專業(yè)術(shù)語搞糊涂了,所以干脆不理它了。
類中的如何定義公共變量,它有什么作用呢?
很簡(jiǎn)單,我們來擴(kuò)充 my_class 類:
class my_class
{
var $username;
}
看上面很簡(jiǎn)單,我們定義了一個(gè)公共的變量,只是用 var+空格+普通變量名 構(gòu)成。它有什么用呢?考慮一下函數(shù)中,如果我們要訪問函數(shù)外的變量,是不是要先 global 一下呢?這個(gè)想實(shí)現(xiàn)的效果也是如此,它是想讓這個(gè)類中的所有函數(shù)都能訪問它,而它區(qū)別于函數(shù)的一個(gè)地方,是類的外部也可以隨時(shí)訪問和控制這個(gè)變量,我隨后再講外部如何訪問它。還有一個(gè)區(qū)別,不能用復(fù)雜的語句給這個(gè)變量賦值具體的等理解了類以后自己去看規(guī)則)。
給它一個(gè)默認(rèn)值:
class my_class
{
var $username = "深空";
}
OK,定義了一個(gè)公共的變量了,接下來定義一個(gè)函數(shù)也就是所謂的“方法”):
class my_class
{
var $username = "深空";
function show_username()
{
}
}
這個(gè)定義函數(shù)跟普通的定義函數(shù)形式上沒什么區(qū)別了。簡(jiǎn)單就好,定義一個(gè)打印 $username 的函數(shù):
class my_class
{
var $username = "深空";
function show_username($username)
{
echo $username;
}
}
到這里可能某些人開始迷糊了,呵呵,最關(guān)鍵的就是這里了,看清楚了。現(xiàn)在有三個(gè) $username 了。到底哪個(gè)是哪個(gè)啊~~
函數(shù)所帶的形參,不用解釋了吧?這個(gè)函數(shù)功能就是打印形參所接收的值,也就是如果:
show_username("豬頭深空");
那么它將打印 “豬頭深空” ,就這么簡(jiǎn)單。
怎么樣訪問這個(gè)函數(shù)?肯定不是我上面說的那樣直接 show_username("豬頭深空"); 了,別急,類有類的一套。如下:
$Name = new my_class();
這樣就初始化上面的那個(gè) my_class 的類了,并把這個(gè)對(duì)象賦給變量 $Name ,你可以這樣理解,這個(gè)變量就代表整個(gè)類了,呵呵。
使用類中的函數(shù):
$Name->show_username("豬頭深空");
暈了,為什么這么復(fù)雜?還要箭頭?其實(shí)很形象的。本來已經(jīng)把類給了變量 $Name 了是吧?也就是 $Name 代表了這個(gè)類,然后用一個(gè)箭頭指向類中的 show_username 這個(gè)函數(shù)。就是這么簡(jiǎn)單,也就是說,這個(gè)函數(shù)是這個(gè)類中的,而不是其他的函數(shù)--你就理解為表示一個(gè)區(qū)別吧,呵呵。
試試看哦,打印出 “豬頭深空” 這四個(gè)字了。你說為什么要這么復(fù)雜?用函數(shù)不是也能實(shí)現(xiàn)么?我說,這么簡(jiǎn)單的你當(dāng)然看不出好處了,我們繼續(xù)擴(kuò)充。
還有一個(gè)疑問是:剛才說的“公共的變量”怎么一點(diǎn)用處都沒有呢?為什么這個(gè)函數(shù)不會(huì)自動(dòng)接收這個(gè)公共變量 var $username 中的默認(rèn)值?也就是如果我使用:
$Name->show_username($username);
會(huì)有什么結(jié)果呢?答案是沒有任何輸出。因?yàn)槟銢]有給形參 $username 一個(gè)值。
那么該怎么使用這個(gè)公共的變量?我們來修改一下這個(gè)類:
class my_class
{
var $username = "深空";
function show_username()
{
echo $this->username;
}
}
哇靠,不是吧,這回連形參都沒有了?還多了一個(gè)$this->,暈了不是,呵呵。其實(shí)這也是類的一個(gè)最大的方便之處。
$this 的作用:訪問一個(gè)公共的變量,或者類里面的函數(shù)。
訪問?這么專業(yè)?其實(shí)就是用 $this->username 來代替 var $username 而已拉,$this 用來說明它是公共的、可以訪問的、函數(shù)外部的東西比如其他變量或函數(shù))。
試試看:
$Name->show_username();
看到了吧,終于打印 “深空” 這兩個(gè)字了,娃哈哈。
我不打印“深空”這兩個(gè)字,我要打印“豬頭深空”,怎么辦?很簡(jiǎn)單,我們給這個(gè)公共變量重新賦值拉。服了你了。
$Name->username = "豬頭深空";
這個(gè)能明白意思么?$Name->username 表示的是類里面的這個(gè)公共變量。等號(hào)賦值不用我解釋了。
我們?cè)賮泶蛴】纯矗?
$Name->show_username();
哈哈,終于打印“豬頭深空”了。不錯(cuò)吧,很方便吧,不用形參也能任意修改打印值哦~~。
不過單單打印一個(gè)名稱也太沒意思了,我們說點(diǎn)歡迎的話吧,來擴(kuò)充一下這個(gè)類,創(chuàng)建一個(gè)名叫 Welcome 的函數(shù):
class my_class
{
var $username = "深空";
function show_username()
{
echo $this->username;
}
function Welcome()
{
}
}
恩,實(shí)現(xiàn)什么功能好呢?簡(jiǎn)單點(diǎn)吧,就實(shí)現(xiàn)在名字前面有 “歡迎” 兩個(gè)字好了
class my_class
{
var $username = "深空";
function show_username()
{
echo $this->username;
}
function Welcome()
{
echo "歡迎";
$this->show_username();
}
}
第二次看到 $this 了吧?和上次有點(diǎn)不同,$this->show_username(); 干什么用呢?指向類中的一個(gè)函數(shù),其實(shí)它就是調(diào)用 show_username 這個(gè)函數(shù),用 $this 來表示這個(gè)函數(shù)在類中并且和 Welcome 函數(shù)平行,而不是在其他地方比如Welcome函數(shù)中)。
Welcome 函數(shù)實(shí)現(xiàn)的功能很簡(jiǎn)單,首先打印兩個(gè)字"歡迎",然后接下去執(zhí)行 show_username 函數(shù),打印名字。
來試試這個(gè)函數(shù)吧:
$Name->Welcome();
看到了吧,打印出“歡迎深空”這四個(gè)字了。
可是我要打印“歡迎豬頭深空”,怎么辦?我服了你了,我們給公共變量 var $username 一個(gè)值吧:
$Name->username = "豬頭深空";
接下去打印歡迎語:
$Name->Welcome();
嘿嘿,終于打印“歡迎豬頭深空”了。
怎么樣?明白了類的用法了么?好處在于能夠調(diào)用類中的任意函數(shù),只要用 $this 指出來,可以改變一個(gè)公共變量的值,可以在類中的函數(shù)中使用這個(gè)公共變量?!嗔巳チ?,它的應(yīng)用等待你去發(fā)現(xiàn)了。

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHPhasthreecommentstyles://,#forsingle-lineand/.../formulti-line.Usecommentstoexplainwhycodeexists,notwhatitdoes.MarkTODO/FIXMEitemsanddisablecodetemporarilyduringdebugging.Avoidover-commentingsimplelogic.Writeconcise,grammaticallycorrectcommentsandu

The key steps to install PHP on Windows include: 1. Download the appropriate PHP version and decompress it. It is recommended to use ThreadSafe version with Apache or NonThreadSafe version with Nginx; 2. Configure the php.ini file and rename php.ini-development or php.ini-production to php.ini; 3. Add the PHP path to the system environment variable Path for command line use; 4. Test whether PHP is installed successfully, execute php-v through the command line and run the built-in server to test the parsing capabilities; 5. If you use Apache, you need to configure P in httpd.conf

The basic syntax of PHP includes four key points: 1. The PHP tag must be ended, and the use of complete tags is recommended; 2. Echo and print are commonly used for output content, among which echo supports multiple parameters and is more efficient; 3. The annotation methods include //, # and //, to improve code readability; 4. Each statement must end with a semicolon, and spaces and line breaks do not affect execution but affect readability. Mastering these basic rules can help write clear and stable PHP code.

The steps to install PHP8 on Ubuntu are: 1. Update the software package list; 2. Install PHP8 and basic components; 3. Check the version to confirm that the installation is successful; 4. Install additional modules as needed. Windows users can download and decompress the ZIP package, then modify the configuration file, enable extensions, and add the path to environment variables. macOS users recommend using Homebrew to install, and perform steps such as adding tap, installing PHP8, setting the default version and verifying the version. Although the installation methods are different under different systems, the process is clear, so you can choose the right method according to the purpose.

PHPisaserver-sidescriptinglanguageusedforwebdevelopment,especiallyfordynamicwebsitesandCMSplatformslikeWordPress.Itrunsontheserver,processesdata,interactswithdatabases,andsendsHTMLtobrowsers.Commonusesincludeuserauthentication,e-commerceplatforms,for

The key to writing Python's ifelse statements is to understand the logical structure and details. 1. The infrastructure is to execute a piece of code if conditions are established, otherwise the else part is executed, else is optional; 2. Multi-condition judgment is implemented with elif, and it is executed sequentially and stopped once it is met; 3. Nested if is used for further subdivision judgment, it is recommended not to exceed two layers; 4. A ternary expression can be used to replace simple ifelse in a simple scenario. Only by paying attention to indentation, conditional order and logical integrity can we write clear and stable judgment codes.

How to start writing your first PHP script? First, set up the local development environment, install XAMPP/MAMP/LAMP, and use a text editor to understand the server's running principle. Secondly, create a file called hello.php, enter the basic code and run the test. Third, learn to use PHP and HTML to achieve dynamic content output. Finally, pay attention to common errors such as missing semicolons, citation issues, and file extension errors, and enable error reports for debugging.

TohandlefileoperationsinPHP,useappropriatefunctionsandmodes.1.Toreadafile,usefile_get_contents()forsmallfilesorfgets()inaloopforline-by-lineprocessing.2.Towritetoafile,usefile_put_contents()forsimplewritesorappendingwiththeFILE_APPENDflag,orfwrite()w
