本文目錄一覽:
PHP多態代碼實例
這篇文章主要介紹了PHP多態代碼實例,本文用2個代碼實例來演示PHP中的多態,需要的朋友可以參考下
多態定義:只關心一個介面或者基類,而不關心一個對象的具體類。(同一類型,不同結果)
這裡兩個例子:
第一個,我們發現,基類定義了標準,子類進行了自我規則的實現。這是多態的一個要求。同時,這是滿足重寫;實際上這是不同類的不同表現;沒有嚴格滿足一個介面,或者基類編程。因為你調用的時候不是
stu-showGrade()
而是各自自己的方法;
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class
stu{
public
function
showGrade(){
echo
base
class;
}
}
class
xiaomin
extends
stu{
public
function
showGrade(){
echo
is
son
show
80;
}
}
class
xiaoli
extends
stu{
public
function
showGrade(){
echo
is
son
show
60;
}
}
function
doit($obj){
if(get_class($obj)
!=
stu){
$obj-showGrade();
}
}
doit(new
xiaoli());
doit(new
xiaomin());
第二個例子:dovoice
參數規定了$obj
為animal,意識就是用介面
接受了
實現類對象。了向上轉型。這就符合同一類型,不同結果了,這就是多態;
實際上在Java中
會是
animal
a
=
new
dog();這樣子的;因為PHP
是若類型語言。沒有對象轉型機制。
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
interface
animal{
public
function
voice();
}
class
cat
implements
animal{
public
function
voice(){
echo
miao~~~br;
}
}
class
dog
implements
animal{
public
function
voice(){
echo
wang
~~~br;
}
}
function
dovoice(animal
$obj){
$obj-voice();
}
dovoice(new
dog());
dovoice(new
cat());
幾種常用PHP連接資料庫的代碼示例
PHP連接資料庫之PHP連接MYSQL資料庫代碼
?php
$mysql_server_name=’localhost’;
//改成自己的mysql資料庫伺服器
$mysql_username=’root’;
//改成自己的mysql資料庫用戶名
$mysql_password=’12345678′;
//改成自己的mysql資料庫密碼
$mysql_database=’mycounter’;
//改成自己的mysql資料庫名
$conn=mysql_connect($mysql_server_name,
$mysql_username,$mysql_password,
$mysql_database);
$sql=’CREATE DATABASE mycounter
DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;
‘;
mysql_query($sql);
$sql=’CREATE TABLE `counter`
(`id` INT(255) UNSIGNED NOT NULL
AUTO_INCREMENT ,`count` INT(255)
UNSIGNED NOT NULL DEFAULT 0,PRIMARY KEY
( `id` ) ) TYPE = innodb;’;
mysql_select_db($mysql_database,$conn);
$result=mysql_query($sql);
//echo $sql;
mysql_close($conn);
echo “Hello!資料庫mycounter已經成功建立!”;
?
PHP連接資料庫之PHP連接ACCESS資料庫代碼方法
?
$conn = new com(“ADODB.Connection”);
$connstr = “DRIVER={Microsoft
Access Driver (*.mdb)};
DBQ=”. realpath(“data/db.mdb”);
$conn-Open($connstr);
$rs = new com(“ADODB.RecordSet”);
$rs-Open(“select *
from szd_t”,$conn,1,1);
while(! $rs-eof) {
$f = $rs-Fields(1);
echo $f-value;
$rs-MoveNext();
}
?
高質量PHP代碼的50個技巧(3)
42
43
44
45
/**
Method to execute a command in the terminal
Uses :
1. system
2. passthru
3. exec
4. shell_exec
*/
function terminal($command)
{
//system
if(function_exists(‘system’))
{
ob_start();
system($command , $return_var);
$output = ob_get_contents();
ob_end_clean();
}
//passthru
else if(function_exists(‘passthru’))
{
ob_start();
passthru($command , $return_var);
$output = ob_get_contents();
ob_end_clean();
}
//exec
else if(function_exists(‘exec’))
{
exec($command , $output , $return_var);
$output = implode(“\n” , $output);
}
//shell_exec
else if(function_exists(‘shell_exec’))
{
$output = shell_exec($command) ;
}
else
{
$output = ‘Command execution not possible on this system’;
$return_var = 1;
}
return array(‘output’ = $output , ‘status’ = $return_var);
}
terminal(‘ls’);
上面的函數將運行shell命令, 只要有一個系統函數可用, 這保持了代碼的一致性.
5. 靈活編寫函數
?
1
2
3
4
5
6
function add_to_cart($item_id , $qty)
{
$_SESSION[‘cart’][‘item_id’] = $qty;
}
add_to_cart( ‘IPHONE3’ , 2 );
使用上面的函數添加單個項目. 而當添加項列表的時候,你要創建另一個函數嗎? 不用, 只要稍加留意不同類型的參數, 就會更靈活. 如:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function add_to_cart($item_id , $qty)
{
if(!is_array($item_id))
{
$_SESSION[‘cart’][‘item_id’] = $qty;
}
else
{
foreach($item_id as $i_id = $qty)
{
$_SESSION[‘cart’][‘i_id’] = $qty;
}
}
}
add_to_cart( ‘IPHONE3’ , 2 );
add_to_cart( array(‘IPHONE3’ = 2 , ‘IPAD’ = 5) );
現在, 同個函數可以處理不同類型的輸入參數了. 可以參照上面的例子重構你的多處代碼, 使其更智能.
6. 有意忽略php關閉標籤
我很想知道為什麼這麼多關於php建議的博客文章都沒提到這點.
?
1
2
3
?php
echo “Hello”;
//Now dont close this tag
這將節約你很多時間. 我們舉個例子:
一個 super_class.php 文件
?
1
2
3
4
5
6
7
8
9
?php
class super_class
{
function super_function()
{
//super code
}
}
?
//super extra character after the closing tag
index.php
?
1
2
require_once(‘super_class.php’);
//echo an image or pdf , or set the cookies or session data
這樣, 你將會得到一個 Headers already send error. 為什麼? 因為 「super extra character」 已經被輸出了. 現在你得開始調試啦. 這會花費大量時間尋找 super extra 的位置。因此, 養成省略關閉符的習慣:
?
1
2
3
4
5
6
7
8
9
?php
class super_class
{
function super_function()
{
//super code
}
}
//No closing tag
這會更好.
7. 在某地方收集所有輸入, 一次輸出給瀏覽器
這稱為輸出緩衝, 假如說你已在不同的函數輸出內容:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
function print_header()
{
echo “p id=’header’Site Log and Login links/p”;
}
function print_footer()
{
echo “p id=’footer’Site was made by me/p”;
}
print_header();
for($i = 0 ; $i 100; $i++)
{
echo “I is : $i ‘;
}
print_footer();
替代方案, 在某地方集中收集輸出. 你可以存儲在函數的局部變數中, 也可以使用ob_start和ob_end_clean. 如下:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function print_header()
{
$o = “p id=’header’Site Log and Login links/p”;
return $o;
}
function print_footer()
{
$o = “p id=’footer’Site was made by me/p”;
return $o;
}
echo print_header();
for($i = 0 ; $i 100; $i++)
{
echo “I is : $i ‘;
}
echo print_footer();
為什麼需要輸出緩衝:
可以在發送給瀏覽器前更改輸出. 如 str_replaces 函數或可能是 preg_replaces 或添加些監控/調試的html內容.
輸出給瀏覽器的同時又做php的處理很糟糕. 你應該看到過有些站點的側邊欄或中間出現錯誤信息. 知道為什麼會發生嗎? 因為處理和輸出混合了.
8. 發送正確的mime類型頭信息, 如果輸出非html內容的話.
輸出一些xml.
?
1
2
3
4
5
6
$xml = ‘?xml version=”1.0″ encoding=”utf-8″ standalone=”yes”?’;
$xml = “response
code0/code
/response”;
//Send xml data
echo $xml;
工作得不錯. 但需要一些改進.
?
1
2
3
4
5
6
7
$xml = ‘?xml version=”1.0″ encoding=”utf-8″ standalone=”yes”?’;
$xml = “response
code0/code
php最經典,最基礎的代碼,適合入門的
PHP是一種可以嵌入到HTML中的運行在伺服器端的腳本語言,所以為了體現PHP的特性我們可以分兩種模式來實現PHP代碼
1、 PHP嵌入到HTML中,例如index.php
html
head/head
body
!–因為PHP嵌入到HTML中,所以需要完全區分PHP代碼和HTML代碼–
?php
//輸出hello world
echo ‘hello world;
?
/body
/html
2、 PHP獨立文件,只有PHP代碼,例如index.php
?php
//輸出
echo ‘hello world’;
//不需要閉合標籤
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/242244.html