Build your own PHP Extension Hanoi PHP Day 2010 Bui Dinh Ngoc AiTi-Aptech - CAH Trưng đào to Lp trình viên Quc t AiTi-Aptech.

Slides:



Advertisements
Similar presentations
PHP I.
Advertisements

Compilation and Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
Overview of programming in C C is a fast, efficient, flexible programming language Paradigm: C is procedural (like Fortran, Pascal), not object oriented.
C Language.
IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
ARDUINO CLUB Session 1: C & An Introduction to Linux.
Kit Chan ATS Lua Plugin Kit Chan Hi, My name is kit.
Unix Continuum of Tools Do something once: use the command line Do something many times: –Use an alias –Use a shell script Do something that is complex.
Chapter 11-12, Appendix D C Programs Higher Level languages Compilers C programming Converting C to Machine Code C Compiler for LC-3.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Guide To UNIX Using Linux Third Edition
CMSC 341 Introduction to Java Based on tutorial by Rebecca Hasti at
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Computer Science 210 Computer Organization Introduction to C.
SJSU CS157B Dr. Lee1  2004 Jenny Mitchell Two Useful Tools You Can’t Live Without by Jenny Mitchell SJSU CS157B Section PHP and MySQL.
NMED 3850 A Advanced Online Design January 26, 2010 V. Mahadevan.
Java Introduction to JNI Prepared by Humaira Siddiqui.
Introduction to Java University of Sunderland CSE301 Harry R. Erwin, PhD.
CSC Java Programming, Fall, 2008 Week 2: Java Data Types, Control Constructs, and their C++ counterparts, September 4.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
PHP By Sergio Rodriguez By Sergio Rodriguez. PHP G PHP: Hypertext Preprocessor G Scripting language G PHP: Hypertext Preprocessor G Scripting language.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
1 Writing a Good Program 8. Elementary Data Structure.
Just a Little PHP Programming PHP on the Server. Common Programming Language Features Comments Data Types Variable Declarations Expressions Flow of Control.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
Comments in PHP In PHP, we use // to make a singleline comment or /* and */ to make a large comment block. Comment is a part of your PHP code that will.
Dr. Ken Hoganson, Kennesaw State University Introduction to Torque Script Programming.
Overview Intro to functions What are functions? Why use functions? Defining functions Calling functions Documenting functions Top-down design Variable.
Java Basics.  To checkout, use: svn co scb07f12/UTORid  Before starting coding always use: svn update.
Strings, output, quotes and comments
Introduction to Java COM379 (Part-Time) University of Sunderland Harry R Erwin, PhD.
Research Topics in Computational Science. Agenda Survey Overview.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
 140 Total Points ◦ 100 Points Writing Programs ◦ 24 Points Tracing Algorithms and determining results ◦ 16 Points Short Answer  Similar to quizzes.
Core Java Introduction Byju Veedu Ness Technologies httpdownload.oracle.com/javase/tutorial/getStarted/intro/definition.html.
©John Samuel 2008 Introduction to PHP. ©John Samuel 2008 Objectives At the end of this class the student will be able to; Create and run a simple php.
הרצאה 4. עיבוד של דף אינטרנט דינמי מתוך Murach’s PHP and MySQL by Joel Murach and Ray Harris.  דף אינטרנט דינמי משתנה עפ " י הרצת קוד על השרת, יכול להשתנות.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
Just a Little PHP Programming PHP on the Server. Common Programming Language Features Comments Data Types Variable Declarations Expressions Flow of Control.
Chapter 1 Introduction to PHP Part 1. Textbook’s Code DOWNLOADS PHP and MySQL for Dynamic Web Sites Complete Set of Scripts.
 Variables can store data of different types, and different data types can do different things.  PHP supports the following data types:  String  Integer.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
1 PHP Intro PHP Introduction After this lecture, you should be able to: Know the fundamental concepts of Web Scripting Languages in general, PHP in particular.
Creating FunctionstMyn1 Creating Functions Function can be divided into two groups: –Internal (built in) functions –User-defined functions.
C:\Temp\Templates 4 5 Use This Main Program 6.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
C is a high level language (HLL)
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
 Prepared by: Eng. Maryam Adel Abdel-Hady
 Prepared by: Eng. Maryam Adel Abdel-Hady
Python C API overview References:
By bscshelp.com 1.  It is a group assignment.  Complete Website design Using Html and Css.  Due date: 10 th December, 2014 (Hard Deadline) 2.
Object Oriented Programming Lecture 2: BallWorld.
2. Java language basics (1)
Basic concepts of C++ Presented by Prof. Satyajit De
dr Robert Kowalczyk WMiI UŁ
Intro to ETEC Java.
Computer Science 210 Computer Organization
UBUNTU INSTALLATION
Introduction to PHP Part 1
C Language By Sra Sontisirikit
Objectives Identify the built-in data types in C++
C Basics.
Server-Side Application and Data Management IT IS 3105 (Spring 2010)
Computer Science 210 Computer Organization
An Introduction to Java – Part I, language basics
SPL – PS1 Introduction to C++.
Presentation transcript:

Build your own PHP Extension Hanoi PHP Day 2010 Bui Dinh Ngoc AiTi-Aptech - CAH Trưng đào to Lp trình viên Quc t AiTi-Aptech

PHP Extension ?

PHP Extension You've used extensions ? php_mysql, gd, pdo, curl,...

PHP Extension (Zend Engine) PHP language written in C PHP interpreter written in C too And PHP Extension must written in C Another PHP implement may be using diffrence language

Why and When need PHP extension ? 1.Buildin PHP function are not enough 2.Existing PHP extension are not enough 3.Pure PHP function are more slow 4.Have C lib can do this for you

Prepare 1.Ubuntu Linux 2.GNU C Compiler, build, make utils 3. PHP 5 Dev package : sudo apt-get install php5-dev 4.PHP source code o sudo svn checkout src/trunk o

PHP-Src-5.3 tree directory

ext_skel.sh script

Write Hello World Extension //Example function call

1.Run ext_skel script : sudo./ext_skel –extname=hello 2.

Result

phpize The phpize command is used to prepare the build environment for a PHP extension.

Edit header file php_hello.h

Insert your function to header file PHP_FUNCTION(hello); /*My function here*/

Edit C source file - pre declare const zend_function_entry simhash_functions[] = { PHP_FE(confirm_hello_compiled, NULL) /* For testing, remove later. */ PHP_FE(hello, NULL) {NULL, NULL, NULL} /* Must be the last line in hello_functions[] */ };

Implement function PHP_FUNCTION(hello) { php_printf(Hello, world!\n); }

Build - Run some script 1.sudo./configure 2.sudo make 3.ls modules -> hello.so

Test 1.Deploy file hello.so 2.Check new extension is loaded by phpinfo function 3.You also can test using existed hello.php script in ext dir

Advance ! 1.Build php function with parameter 2.Return value 3.Memory allocation 4.Anti Memory leak 5.Array 6.String 7.Global variable 8.PHP.INI variable

Function with parameter function hello_add($a, $b) { $sum = (int)$a + (float)$b; return $sum; }

Function with parameter PHP_FUNCTION(hello_add) { long a; double b; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ld", &a, &b) == FAILURE) { RETURN_NULL(); } RETURN_DOUBLE(a + b); }

Return value 1.bool 2.int 3.double 4.resource 5.array 6.object Only 6 return type

Return value (macro) RETURN_LONG() for integer values RETURN_DOUBLE() for floating point values RETURN_BOOL() for true/false values RETURN_NULL() for null value.....

Memory allocation

Anti Memory leak In C, memory management always very hard. Wrapper functions provides you with a safety net and some helpful debugging facilities But convert existing C source can't use wrapper functions

Reference "Programming PHP" by Rasmus Lerdorf and Kevin