Home Backend Development PHP Tutorial zephir-(12)php函数和异常处理

zephir-(12)php函数和异常处理

Jun 20, 2016 pm 12:34 PM

#zephir-php函数和异常处理#![](http://i.imgur.com/OuZmZ0R.png)##前言##***先在这里感谢各位zephir开源技术提供者***经过了一个多月的学习,zephir的文档译文和基础讲解也将近尾声了,后面的内容最为重要也希望和大家一同更好的学习交流,本节的内容只要是讲解zephir是怎么时候PHP自带的函数库已经特德异常机制处理的,那么让我们开始本节的旅程把.**注:笔者水平有限,说的不正确的地方希望大家多多指正,一同交流技术**附上:喵了个咪的博客:[w-blog.cn](w-blog.cn)zephir官网地址:[http://zephir-lang.com/](http://zephir-lang.com/ "zephir官网")github地址:[https://github.com/phalcon/zephir](https://github.com/phalcon/zephir)##php函数##PHP有一个丰富的函数库,您可以使用在你的扩展。 调用PHP函数只需正常使用它在你Zephir代码:	namespace MyLibrary;		class Encoder	{		    public function encode(var text)	    {	        if strlen(text) != 0 {	            return base64_encode(text);	        }	        return false;	    }	}你可以调用用户建立的php函数:	namespace MyLibrary;		class Encoder	{		    public function encode(var text)	    {	        if strlen(text) != 0 {	            if function_exists("my_custom_encoder") {	                return my_custom_encoder(text);	            } else {	                return base64_encode(text);	            }	        }	        return false;	    }	}注意所有PHP函数只接收和返回动态变量。 如果你通过静态类型变量作为参数,一个临时的动态变量将自动被用作桥为了调用函数:	namespace MyLibrary;		class Encoder	{		    public function encode(string text)	    {	        if strlen(text) != 0 {	            return base64_encode(text);	        }	        return false;	    }	}类似地,函数返回动态值不能直接分配给静态变量:	namespace MyLibrary;		class Encoder	{		    public function encode(string text)	    {	        string encoded = "";		        if strlen(text) != 0 {	            let encoded = (string) base64_encode(text);	            return '(' . encoded . ')';	        }	        return false;	    }	}Zephir动态提供了一种方法来调用函数,如:	namespace MyLibrary;		class Encoder	{		    public function encode(var callback, string text)	    {	        return {callback}(text);	    }	}##异常处理##Zephir实现异常在很低的水平,为PHP提供类似的行为和功能。抛出异常时,可以使用“捕捉”块捕获异常并允许 开发人员提供适当的处理。	try {		    //异常都可以在这里抛出	    throw new \Exception("This is an exception");		} catch \Exception, e {		    //处理异常	    echo e->getMessage();	}Zephir提供了一直没有反应的“try”,简单地忽略任何异常在那块:	try {	    throw new \Exception("This is an exception");	}一个“catch ”块可以用来捕获不同类型的异常:	try {		    //异常都可以在这里抛出	    throw new \Exception("This is an exception");		} catch RuntimeException|Exception, e {		    //处理异常	    echo e->getMessage();	}Zephir允许你把文字或静态类型化变量当作异常的消息:	throw "Test";   // throw new \Exception("Test");	throw 't';      // throw new \Exception((string) 't');	throw 123;      // throw new \Exception((string) 123);	throw 123.123;  // throw new \Exception((string) 123.123);Zephir作为PHP的异常提供相同的设施,让你知道发生了异常。 例外::getFile()和异常:getLine()返回位置Zephir代码已经被抛出的异常:		Exception: The static method 'someMethod' doesn't exist on model 'Robots'	File=phalcon/mvc/model.zep Line=4042	#0 /home/scott/test.php(64): Phalcon\Mvc\Model::__callStatic('someMethod', Array)	#1 /home/scott/test.php(64): Robots::someMethod()	#2 {main}##总结##本节主要讲解了,zephir可以直接使用PHP的函数库并且可以直接调用PHP用户定义的函数,其实这个是对PHP开发这最方便的一件事情,还有对异常处理进行了一些粗略的讲解,那么今天的zephir译文和讲解就到这里了,多谢大家的支持!注:笔者能力有限有说的不对的地方希望大家能够指出,也希望多多交流!**zephir技术交流:246348908 欢迎大家的加入!****感谢zephir开发人员:**![](http://i.imgur.com/puoG4mx.png)
Copy after login

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)

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

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 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.

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...

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...

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.

See all articles