国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home php教程 php手冊(cè) 關(guān)于PHP中的Class

關(guān)于PHP中的Class

Jun 13, 2016 am 10:19 AM
class php by author about source Deep space View Talk about it transcend

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

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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to access a character in a string by index in PHP How to access a character in a string by index in PHP Jul 12, 2025 am 03:15 AM

In PHP, you can use square brackets or curly braces to obtain string specific index characters, but square brackets are recommended; the index starts from 0, and the access outside the range returns a null value and cannot be assigned a value; mb_substr is required to handle multi-byte characters. For example: $str="hello";echo$str[0]; output h; and Chinese characters such as mb_substr($str,1,1) need to obtain the correct result; in actual applications, the length of the string should be checked before looping, dynamic strings need to be verified for validity, and multilingual projects recommend using multi-byte security functions uniformly.

How Do Generators Work in PHP? How Do Generators Work in PHP? Jul 11, 2025 am 03:12 AM

AgeneratorinPHPisamemory-efficientwaytoiterateoverlargedatasetsbyyieldingvaluesoneatatimeinsteadofreturningthemallatonce.1.Generatorsusetheyieldkeywordtoproducevaluesondemand,reducingmemoryusage.2.Theyareusefulforhandlingbigloops,readinglargefiles,or

How to prevent session hijacking in PHP? How to prevent session hijacking in PHP? Jul 11, 2025 am 03:15 AM

To prevent session hijacking in PHP, the following measures need to be taken: 1. Use HTTPS to encrypt the transmission and set session.cookie_secure=1 in php.ini; 2. Set the security cookie attributes, including httponly, secure and samesite; 3. Call session_regenerate_id(true) when the user logs in or permissions change to change to change the SessionID; 4. Limit the Session life cycle, reasonably configure gc_maxlifetime and record the user's activity time; 5. Prohibit exposing the SessionID to the URL, and set session.use_only

How to URL encode a string in PHP with urlencode How to URL encode a string in PHP with urlencode Jul 11, 2025 am 03:22 AM

The urlencode() function is used to encode strings into URL-safe formats, where non-alphanumeric characters (except -, _, and .) are replaced with a percent sign followed by a two-digit hexadecimal number. For example, spaces are converted to signs, exclamation marks are converted to!, and Chinese characters are converted to their UTF-8 encoding form. When using, only the parameter values ??should be encoded, not the entire URL, to avoid damaging the URL structure. For other parts of the URL, such as path segments, the rawurlencode() function should be used, which converts the space to . When processing array parameters, you can use http_build_query() to automatically encode, or manually call urlencode() on each value to ensure safe transfer of data. just

PHP get the first N characters of a string PHP get the first N characters of a string Jul 11, 2025 am 03:17 AM

You can use substr() or mb_substr() to get the first N characters in PHP. The specific steps are as follows: 1. Use substr($string,0,N) to intercept the first N characters, which is suitable for ASCII characters and is simple and efficient; 2. When processing multi-byte characters (such as Chinese), mb_substr($string,0,N,'UTF-8'), and ensure that mbstring extension is enabled; 3. If the string contains HTML or whitespace characters, you should first use strip_tags() to remove the tags and trim() to clean the spaces, and then intercept them to ensure the results are clean.

PHP get the last N characters of a string PHP get the last N characters of a string Jul 11, 2025 am 03:17 AM

There are two main ways to get the last N characters of a string in PHP: 1. Use the substr() function to intercept through the negative starting position, which is suitable for single-byte characters; 2. Use the mb_substr() function to support multilingual and UTF-8 encoding to avoid truncating non-English characters; 3. Optionally determine whether the string length is sufficient to handle boundary situations; 4. It is not recommended to use strrev() substr() combination method because it is not safe and inefficient for multi-byte characters.

How to set and get session variables in PHP? How to set and get session variables in PHP? Jul 12, 2025 am 03:10 AM

To set and get session variables in PHP, you must first always call session_start() at the top of the script to start the session. 1. When setting session variables, use $_SESSION hyperglobal array to assign values ??to specific keys, such as $_SESSION['username']='john_doe'; it can store strings, numbers, arrays and even objects, but avoid storing too much data to avoid affecting performance. 2. When obtaining session variables, you need to call session_start() first, and then access the $_SESSION array through the key, such as echo$_SESSION['username']; it is recommended to use isset() to check whether the variable exists to avoid errors

How to prevent SQL injection in PHP How to prevent SQL injection in PHP Jul 12, 2025 am 03:02 AM

Key methods to prevent SQL injection in PHP include: 1. Use preprocessing statements (such as PDO or MySQLi) to separate SQL code and data; 2. Turn off simulated preprocessing mode to ensure true preprocessing; 3. Filter and verify user input, such as using is_numeric() and filter_var(); 4. Avoid directly splicing SQL strings and use parameter binding instead; 5. Turn off error display in the production environment and record error logs. These measures comprehensively prevent the risk of SQL injection from mechanisms and details.

See all articles