How to create custom PHP extension?

Required Tools

In order to create custom PHP extension, requires the following build tools
  • C/C++ compiler (For example, GCC).
  • phpize which is a shell script to prepare PHP extension for compiling.
  • php-config which is a simple shell script for obtaining information about the installed PHP configuration.
On RPM based systems, required packages can be installed using following command:
$ yum install gcc php php-devel

On Debian based systems, required packages can be installed using following command:
$ apt-get install gcc php php5-dev

Create config.m4 file 

PHP_ARG_ENABLE(my_extension, whether to enable my extension, 
[ --enable-my-extension Enable my extension])
if test "$PHP_MY_EXTENSION" = "yes"; then
 AC_DEFINE(HAVE_MY_EXTENSION, 1, [Whether you have my extension])
 PHP_NEW_EXTENSION(my_extension, my_custom_extension.c, $ext_shared)
fi


The config.m4 file for an extension tells the UNIX build system 
  • what configure options your extension supports
  • what external libraries and includes you require
  • what source your require

This config.m4 stores the basic configuration data used by the PHP 
to compile your custom extension.

Create my_custom_extension.c 

#ifdef HAVE_CONFIG_H

#include "config.h"
#endif
#include "php.h"

#define PHP_MY_EXTENSION_VERSION "1.0"
#define PHP_MY_EXTENSION_EXTNAME "my_extension"

extern zend_module_entry my_extension_module_entry;
#define phpext_my_extension_ptr &my_extension_module_entry
// declaration of a custom my_function()
PHP_FUNCTION(my_function);
// list of custom PHP functions provided by this extension
// set {NULL, NULL, NULL} as the last record to mark the end of list
static function_entry my_functions[] = {
PHP_FE(my_function, NULL)
{NULL, NULL, NULL}};

// the following code creates an entry for the module and registers it with Zend.
zend_module_entry my_extension_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
PHP_MY_EXTENSION_EXTNAME,
my_functions,
NULL, // name of the MINIT function or NULL if not applicable
NULL, // name of the MSHUTDOWN function or NULL if not applicable
NULL, // name of the RINIT function or NULL if not applicable
NULL, // name of the RSHUTDOWN function or NULL if not applicable
NULL, // name of the MINFO function or NULL if not applicable
#if ZEND_MODULE_API_NO >= 20010901
PHP_MY_EXTENSION_VERSION,
#endif
STANDARD_MODULE_PROPERTIES
};
ZEND_GET_MODULE(my_extension)
// implementation of a custom my_function()
PHP_FUNCTION(my_function) {
RETURN_STRING("A line from my custom php extension", 1);
}

Building the extension

$ phpize
$ ./configure
$ make install
  • The phpize command will generate additional configuration  files, required to compile your PHP extension 
  • you can reverse this process anytime by typing the “phpize –clean” command
  • After running each of these commands, you should have a my_custom_extension.so file in PHP extensions directory.
If don’t know the name of that directory, you can check it’s name by typing:
$ php-config | grep extension-dir

Configure php.ini

The last thing you need to do, is to add the following line to your php.ini to load your extension on PHP startup:
extension=my_extension.so

Testing the extension

You can test your PHP extension by typing the following command:
$ php -r "echo my_function();"

If everything was fine, the output will be:
A line from my custom php extension


25 comments:

  1. If you want to learn wordpress then start from here.I have create a tutorial with screen shots so that you can easy understand.
    Learn wordpress theme development from scratch

    ReplyDelete
  2. The data gave here was of high caliber and valuable. A debt of gratitude is in order for the instructive post gave. Website Design Company Bangalore | Website Development Company Bangalore

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Nice blog..! I really loved reading through this article. Thanks for sharing such
    a amazing post with us and keep blogging...Well written article Thank You for Sharing with Us pmp training Chennai | pmp training centers in Chenai | pmp training institutes in Chennai | pmp training and certification in Chennai | pmp training in velachery

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. java
    Very good information. Its very useful for me. We need learn from real time examples and for this we choose good training institute, we need to learn from experts . So we make use of demo classes . Recently we tried java demo class of Apponix Technologies.
    https://www.apponix.com/Java-Institute/Java-Training-Institute-in-Bangalore.html

    ReplyDelete

  7. Get all Technology Updates Learn latest technology, software development skills, b.tech, MCA and BCA computer

    programming training.

    ReplyDelete
  8. very nice blogs!!! i have to learning for lot of information for this sites...Sharing for wonderful information. Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing

    Digital Marketing In Telugu
    Digital Marketing In Hyderabad
    internet marketing
    Digital marketing
    Digital Marketing Strategy

    ReplyDelete
  9. Usually I never comment on blogs but your article is so convincing that I never stop myself to say something about it. You’re doing a great job,Keep it up.

    Try Our Vashikaran Expert In Toronto Services and Get All the benefits of it in your life, we make All your Personal problems solved in just minutes.

    ReplyDelete
  10. Enjoyed reading the article above, really explains everything in detail, the article is very interesting and effective. Thank you and good luck for the upcoming articles.

    Digital Marketing Training in Chennai

    Digital Marketing Course in Chennai


    ReplyDelete
  11. The best Natural Treatment of Atrial Fibrillation includes diet, exercise, yoga and herbs. A good nutrition is completely essential for good hearth. If you think that a petrol engine will not run on diesel fuel, it's easy to understand this idea. If you don't worry good food, then your body cannot help you as it is erected to. It lacks the necessary nutrients to function truthfully.

    ReplyDelete
  12. This comment has been removed by the author.

    ReplyDelete
  13. This comment has been removed by the author.

    ReplyDelete
  14. Natural Treatment for Emphysema is helpful in clearing the air passages in the lungs. Herbal Supplement is to provide relief of symptoms and prevent complications.

    ReplyDelete
  15. First I want to tell you Thank you for sharing it with us.
    I am really Happy and impressed to see this blog!
    law essay writing help

    ReplyDelete
  16. Amazing!
    Such a wonderful and helpful blog for me.
    Thank you for sharing it with us.
    Buy Law Essay UK

    ReplyDelete
  17. Borgata Hotel Casino & Spa - Mapyro
    Free WiFi and free parking at 순천 출장마사지 Borgata Hotel 경기도 출장샵 Casino & Spa, Atlantic City. 청주 출장안마 8 영천 출장샵 Restaurants and a Bar with 서산 출장안마 Coffee Bar, Belly and the Night Club, Borgata Hotel Casino

    ReplyDelete

  18. A hydrocele is a fluid-filled sac that surrounds a testicle and produces enlargement in the scrotum. The majority of hydrocele cases are caused by birth, and newborn kids are at a greater risk of developing hydrocele. It mostly affects males rather than women. They may strike men of any age. So Organic Herbal Supplements are the best option to cure from that disease.

    ReplyDelete
  19. Herbal Supplement for Actinic Keratosis is highly recommended to all people suffering from these diseases as they offer positive results at an affordable price. Herbal Remedies for Actinic Keratosis, It immediately reduced redness.The extract of olive leaf has been used for thousands of years to treat various skin conditions. Herbal Remedies for Actinic Keratosis, It immediately reduced redness.

    ReplyDelete
  20. I always look forward to seeing your latest creations. They never disappoint.
    scilnbio.com

    ReplyDelete
  21. Extremely intriguing to peruse this article.I might want to thank you for the endeavors you had made for composing this marvelous article. This article propelled me to understand more. keep it up.
    our sclinbio.com

    ReplyDelete
  22. "HII
    Nice Article
    Thanks to share with us

    Looking to kickstart your career in SAP BASIS? Bangalore offers a plethora of options for SAP BASIS training.
    Here’s what to consider:
    SAP BASIS Training in Bangalore at SAP Masters
    1.Quality Training:
    Opt for best institutes with a solid reputation and accreditation from SAP Masters Training institute.
    Look for expert faculty who can provide comprehensive insights.
    2.Curriculum:Ensure the curriculum covers core BASIS concepts and offers hands-on projects for practical learning.
    3.Infrastructure: Check for modern facilities and labs equipped with the latest software to support your training needs.
    4.Placement Support: Choose institutes that offer robust placement assistance, including resume building and interview preparation.
    Consider institutes like SAP Masters Institute of Technology, sap masters Academy, and sapmasters training institute bangalore,
    known for their quality training and successful placements. Choose wisely, and jumpstart your SAP BASIS journey in Bangalore!


    Visit SAP Masters - Best SAP Training in Bangalore"
    Visit SAP Masters - SAP BASIS Training in Bangalore"

    ReplyDelete