Table of Contents
The OOP page
Home Backend Development PHP Tutorial PHP_Bibel阅读笔记(二)--脸黑的一天(?一年)

PHP_Bibel阅读笔记(二)--脸黑的一天(?一年)

Jun 23, 2016 pm 01:08 PM

  一早上起来把50包开了,一张橙卡。。。就问还有谁。。。。。。。。。。。本命年啊,我去买红内裤还不行么。。。。

实时更新,老哥的号的30包什么都没有。。。。不过中午又开了5包,皇帝,好评啊!!!

五、代码重用与函数编写

include警告与require的错误;

大量的包含实现,可以改ini文件中的:auto_prepend_file和auto_append_file;

global关键字也是可以用在函数内的参数的;

参数的引用传递和return

1 namespace Bible\Basic\FunctionUse;2 3 include_once 'index.php';4 $value=10;5 increment1($value);6 echo $value;echo "<br/>";7 $value2=100;8 $value2=increment2($value2);9 echo $value2;echo "<br/>";
Copy after login

 1 <?php 2 function increment1(&$value,$mount=1) 3 { 4     $value=$value+$mount; 5 } 6  7 function increment2($value,$mount=1) 8 { 9     $value=$value+$mount;10     return $value;11 }
Copy after login

递归函数比循环慢且占用更多内存,虽然很多循环都可以用递归来代替;(在应用领域中基本不用它

六、面向对象

好玩的代码,学到些东西!

  1 <?php  2 namespace Bible\Basic\ObjectPage;  3   4 //Chapter6.P132  5   6 class Page  7 {  8     public $content;  9     public $title; 10     public $keyword; 11     public $button=array("Home"=>"http://115.159.201.78/wordpress/", 12         "Basic"=>"Section1.php" 13     ); 14      15     public function __set($name,$value) 16     { 17         $this->$name=$value; 18     } 19      20     public function Display() 21     { 22         echo "<html>\n<head>\n"; 23         $this->DisplayTitle(); 24         $this->DisplayKeywords(); 25         $this->DisplayStyles(); 26         echo "</head>\n<body>\n"; 27         $this->DisplayHeader(); 28         $this->DisplayMenu($this->button); 29         echo $this->content; 30         $this->DisplayFooter(); 31         echo "</body>\n</html>\n";    32     } 33      34     private function DisplayTitle() 35     { 36         echo "<title>".$this->title."</title>"; 37     } 38      39     private function DisplayKeywords() 40     { 41         foreach ($this->keyword as $words){ 42             echo "<meta name=\"keywords\" content=\"".$words."\"/>"; 43         } 44     } 45      46     private function DisplayStyles() 47     { 48    ?> 49      <style> 50       h1 { 51             color:white; font-size:24pt; text-align:center; 52             font-family:arial,sans-serif 53     } 54     .menu { 55         color:white; font-size:12pt; text-align:center; 56         font-family:arial,sans-serif; font-weight:bold 57     } 58     td { 59         background:black 60     } 61     p { 62         color:black; font-size:12pt; text-align:justify; 63         font-family:arial,sans-serif 64     } 65     p.foot { 66         color:white; font-size:12pt; text-align:center; 67         font-family:arial,sans-serif; font-weight:bold 68     } 69     a:link,a:visited,a:active { 70         color:white 71     } 72     </style> 73 <?php 74     } 75      76     private function DisplayHeader() 77     { 78 ?> 79   <table width="100%" cellpadding="12"  80          cellspacing="0" border="0"> 81   <tr bgcolor="black"> 82     <td align="left"><img src="/static/imghw/default1.png"  data-src="logo.png"  class="lazy"  alt="img"></td> 83     <td><h1 id="The-OOP-page">The OOP page</h1></td> 84   </tr> 85 </table> 86 <?php  87     } 88      89     private function DisplayMenu($button) 90     { 91         echo "<table width=\"100%\" bgcolor=\"white\" 92               cellpadding=\"4\"    cellsapcing=\"4\">\n "; 93         echo "<tr>\n"; 94         $width=100/count($button); 95         while (list($name,$url)=each($button)) { 96             $this->DisplayButton($width,$name,$url, 97                 !$this->IsURLCurrentPage($url)); 98         } 99         echo "</tr>\n";100         echo "</table>\n";101     }102     103     private function IsURLCurrentPage($url)104     {105         if (strpos($_SERVER['PHP_SELF'], $url)==false){106             return false;107         } else{108             return true;109         }110     }111     112     private function DisplayButton($width,$name,$url,$active=true)113     {114         if ($active){115             echo "<td width=\"".$width."%\">116             <a href=\"".$url."\" onclick=\"blank\">117             <img src=\"w-logo-blue.png\" alt=\"".$name."\" border=\"0\" /></a>118             <a href=\"".$url."\"><span class=\"menu\">".$name."</span></a>119                 </td>";120         } else{121             echo "<td width=\"".$width."%\">122             <img  src=\"w-logo-white.png\" alt="PHP_Bibel阅读笔记(二)--脸黑的一天(?一年)" >123             <span class=\"menu\">".$name."</span></td>";            124         }        125     }126     127     private function DisplayFooter()128     {129 ?>130   <table width="100%" bgcolor="black" cellpadding="12" border="0">131   <tr>132     <td>133     <p class="foot">&copy; Andy Liang.</p>134     <p class="foot">Please visit my own site:<a href="http://115.159.201.78/wordpress/">Andy's Learning Diary</a></p>135     </td>136   </tr>137 </table>138 <?php 139     }140 }141 142 $homepage=new Page();143 $homepage->content="<p>"."I do not know what to write down, how about this?"."</p>";144 $homepage->title="You have to try OO.";145 $homepage->keyword=array("SAR","MRF");146 $homepage->Display();
Copy after login

就是个默认网页的生成对象,还是有改进空间的。

然后,php写html,有点繁琐,但写完了一身爽。

标记的活用有点厉害啊,套路~

PS:上面代码中的链接不要乱点~会吓到你的。

当然,其实这种得到页面在应用中是不推荐的,只是试着练下手。

七、错误和异常处理

try{ throw new Exception() } catch{ }

PHP中,异常必须手动抛出;

try代码块和catch代码块是“绑定的”,每个try一定要有一个catch!

一个try可以有多个catch

 1 <?php 2 //Session1.Chapter7.P146 3 namespace Bible\Basic\ExceptionTry; 4  5 try { 6     throw new \ErrorException("A serious wrong has occured!", 14); 7 } 8 catch (\ErrorException $e){ 9     echo "Exceotion".$e->getCode().":".$e->getMessage()."<br/>"10         ."File:".$e->getFile()."at Line:".$e->getLine()."<br/>";11     echo $e;    12 }
Copy after login

应用中,希望可以自定义异常处理:继承已有的Exception类就好,需要注意的是,一般的getMessage等是final的,不能进行重载的,只有_tostring这一个方法可以重载;

 1 class MyException extends \ErrorException 2 { 3     function _tostring() 4     { 5         return "Fatal error,sorry!"; 6     } 7 } 8  9 try {10     throw new MyException("OH", 14);11 }12 catch (MyException $e){13     echo $e->_tostring();14 }
Copy after login

 

应用中,常常把异常处理用在最容易出错的I/O部分;格式一般都是

try{ if(!...) throw...} catch(){}

 

Session 2

(二) 使用MySQL

八、设计Web数据库

 

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

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.

Undress AI Tool

Undress AI Tool

Undress images for free

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 does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

See all articles