search
HomeBackend DevelopmentPHP Tutorialzephir-(3)你的第一个PHP拓展

#zephir-你的第一个PHP拓展#![](http://i.imgur.com/OuZmZ0R.png)##前言##***先在这里感谢各位zephir开源技术提供者***在之前的介绍中大家不仅了解了zephir具体是一个什么样的语言,也get到了它的安装方式,成功安装到自己的系统上了.今天让我们来一同编写之前我们演示过的一些拓展让我们来一同动起手来尝试一下.**注:笔者水平有限,说的不正确的地方希望大家多多指正,一同交流技术**附上:喵了个咪的博客:[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)##1. 第一个拓展##Zephir和这本书(官方文档和此译文讲解)的目的是为PHP开发人员想要用较低的复杂性创建c扩展。假设你有一个或多个其他编程语言的经验,我们将指出zephit和PHP中,C、Javascript和其他语言有很多相似的特性,以及新的或不同的许多特性。如果你已经成功安装Zephir,你必须能够在您的控制台执行以下命令:	$ zephir help如果一切都好,你应该在你的屏幕上看到以下帮助信息.###1.1 扩展框架###我们要做的第一件事就是生成一个扩展框架,这将提供给我们扩展的基本 我们需要开始工作的结构。 在我们的例子中,我们将创建一个扩展名为“utils”:	$ zephir init utils执行之后,一个目录称为“utils”创建在当前工作目录:	$ cd utils	$ ls	ext/ utils/ config.json目录“ext/”包含将要由编译器用于产生扩展的代码。创建的另一个目录是“utils”,在utils目录下面还有一个utils目录,我们将我们编写的Zephir代码放在在这个目录中。目录列表也将向我们展示一个文件称为“config.json”,该文件包含配置设置 我们可以用它来改变Zephir扩展的行为以及一些扩展信息。###1.2 我们的第一节课###Zephir生成面向对象的扩展。开始开发,我们需要给我们的一个类添加到扩展功能。正如在许多语言/工具,我们要做的第一件事就是写一个“Hello World”对zephir进行检查,因此,我们的第一个类将被称为“Utils\Greeting”,它包含的方法打印“hello world!”.。这个类的代码必须被放置在“utils/utils/greeting.zep”:	namespace Utils;		class Greeting	{		    public static function say()	    {	        echo "hello world!";	    }		}现在,我们需要告诉Zephir编译和生成的扩展,必须在根目录:	$ zephir build当然我们在编译中可能遇到很多问题比如:	PHP Warning:  shell_exec().....这是应为php.ini没有打开这些函数导致无法使用这些函数,我们找到**disable_functions = **然后去掉后面的**exec和shell_exec,passthr,system**zephir的编译依赖于这些函数还有可能可能会看到如下提示:	error: re2c is not installed这是应为没有安装re2c拓展,执行如下语句安装:		wget http://downloads.sourceforge.net/project/re2c/0.15.3/re2c-0.15.3.tar.gz	tar zxf re2c-0.15.3.tar.gz && cd re2c-0.15.3	./configure	make && make install如果一切顺利,您将看到以下消息 的输出:		...	Extension installed!	Add extension=utils.so to your php.ini	Don't forget to restart your web server最后,必须添加到php扩展。 php.ini中加入extension=utils.so。现在,添加到您的php扩展。 ini,检查是否正常加载扩展通过执行以下:	$ php -m	[PHP Modules]	Core	date	libxml	pcre	Reflection	session	SPL	standard	tokenizer	utils	xdebug	xml扩展“utils”必须的一部分输出表明扩展是正确加载。 现在,让我们看看我们 “hello world”直接执行的PHP。 为此,您可以创建一个简单的PHP文件调用静态方法 刚刚创建的:	<?php		echo Utils\Greeting::say(), "\n";恭喜你! ,你第一次上运行PHP扩展。##2 一个有用的类##“hello world”类很好检查如果我们的环境是对的,现在,让我们创建一些更有用的类。第一个有用的类,我们要添加此扩展将为用户提供过滤设备。 这个类被称为“Utils\Filter”,其代码都必须放置在“utils/Utils/filter.zep”:	namespace Utils;		class Filter	{		}类包含过滤方法,帮助用户过滤不必要字符串。 第一个方法叫做“alpha”,其目的是过滤只有那些ascii字符基本的字母。 开始,我们只是将遍历每个字节字符串打印到标准输出:	namespace Utils;		class Filter	{		    public function alpha(string str)	    {	        char ch;		        for ch in str {	            echo ch, "\n";	        }	    }	}当调用这个方法:	<?php		$f = new Utils\Filter();	$f->alpha("hello");你会看到:	h	e	l	l	o检查每个字符的字符串都是简单的,我们现在就可以创建另一个字符串过滤字符:	class Filter	{		    public function alpha(string str) -> string	    {	        char ch; string filtered = "";		        for ch in str {	            if (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') {	                let filtered .= ch;	            }	        }		        return filtered;	    }	}完整的方法也可以像之前测试:	<?php		$f = new Utils\Filter();	echo $f->alpha("!he#02l3'121lo."); ##3. 总结##这是一个非常简单的教程,你可以看到,很容易使用Zephir开始构建扩展,后面我们更深入的学习会和所有语言一样从最基础变量语法开始,一步一步深入,最后多谢大家的支持!注:笔者能力有限有说的不对的地方希望大家能够指出,也希望多多交流!**zephir技术交流:246348908 欢迎大家的加入!****感谢zephir开发人员:**![](http://i.imgur.com/puoG4mx.png)

Statement
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

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

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools