Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building custom PHP extension (.so)

Tags:

php

I own a high traffic website that does business in the USA and Canada. We have lots of servers but I want to make sure it's 100% available with no latency whatsoever.

I've learned about creating custom extensions (I know a little C) and I want to create custom validation/files (since php extensions runs faster).

I don't want to ask for all new extensions from you guys but I want to know the general idea on how to build it (I am using CentOS).

Example:

One section of our site is the shipping tracking and this require a postal code.

For the USA I have:

function check_usa_postal_code($pc) {
  return is_numeric($pc);
}

But for Canada, I would like to build in PHP a custom function like:

check_canada_postal_code($pc)

This function should return 1 or 0.

Thanks

like image 857
apollo Avatar asked Nov 23 '11 12:11

apollo


2 Answers

You should also consider using Zephir. Its syntax is very similar to PHP. It is specially designed to create PHP extensions and compiles into an extension.

There are even several converters from regular PHP to Zephir. They do not take into account all the peculiarities, so some minor code changes will be needed before compiling. I use sandrokeil/php-to-zephir.

like image 193
JWprogrammer Avatar answered Oct 20 '22 23:10

JWprogrammer


I recommend to read this article:

Extension Writing Part I: Introduction to PHP and Zend

and here's how to compile:

UNIX: Compiling PHP Extensions

Build PHP extensions with SWIG

(I recommend SWIG like mario said)

Read more on Canadian zip codes at Postal codes in Canada since not all letters are being used.

like image 20
Book Of Zeus Avatar answered Oct 20 '22 23:10

Book Of Zeus