1
|
- Jon Parise <jon@php.net>
- 2002 International PHP Conference
- Frankfurt, Germany
- November 6, 2002
|
2
|
- Some familiarity with PHP extensions is expected.
- Python knowledge is not required, but familiarity will be helpful.
|
3
|
- Bachelor of Science in Information Technology from the Rochester
Institute of Technology
- Completing Masters of Entertainment Technology at Carnegie Mellon
University
- Software engineer at Maxis on The Sims Online
- Long history of involvement with PHP, PEAR, and The Horde Project
- Co-author of Professional PHP4 Programming
- Long-time Pythonista!
|
4
|
- Questions
- Ask for clarification at any time.
- Please save scope-expanding questions until the end.
- Pacing
- Ask me to slow down if I move too quickly.
- I’m from New Jersey.
|
5
|
- Overview
- Extension architecture
- Type conversions
- Object handling
- PHP Python Module
- Next Steps
- Questions
|
6
|
|
7
|
- Embedded Python interpreter
- Interface handled by PHP extension
- Python-to-PHP object proxy
- Handles type conversions
- Exposes PHP environment to Python
|
8
|
|
9
|
|
10
|
- PHP starts and initializes the Python extension.
- The Python extension initializes the Python interpreter.
- Python-related operations are performed in PHP.
- PHP shuts down the Python extension, which cleans up the Python
interpreter.
|
11
|
|
12
|
|
13
|
- Extension initialization
- Python initialization
- Python code execution
- Extension shutdown
- Python shutdown
- PHP_MINIT_FUNCTION
- Py_Initialize()
- PyRun_SimpleString()
- PHP_MSHUTDOWN_FUNCTION
- Py_Finalize()
|
14
|
- Executes a string of Python code
- Uses PyRun_SimpleString()
- Only returns success or failure
- Always executes in the same Python environment
|
15
|
|
16
|
- Calls a function of a module
- Uses PyObject_CallObject()
- Implicitly imports the module
- Allows parameter passing
- Returns the result of the function call
|
17
|
- PHP
- Boolean
- Long (Integer)
- Double (Float)
- String
- Null
- Python
- Integer
- Long
- Double
- String
- None
|
18
|
- Python
- Integer
- Long
- Float
- String
- None
- PHP
- Long
- Long
- Double
- String
- NULL
|
19
|
- PHP only has hashes, indexed by:
- Numbers: array(1, 2)
- Strings: array('one' => 1, 'two' => 2)
- Python has sequences:
- Tuples: (1, 2)
- Lists: [1, 2]
- And mappings:
- Dictionaries: {'one': 1, 'two': 2}
|
20
|
- PHP arrays (hashes) are always converted to Python dictionaries.
- Results in no data loss.
|
21
|
- Arrays of arguments are always passed as a tuple.
- String keys are discarded.
|
22
|
- Python tuples and lists are always converted to PHP arrays.
- The numerical indices are preserved.
|
23
|
- Python dictionaries are always converted to PHP associative arrays.
- Keys will be converted to strings.
|
24
|
- The Python extension proxies Python objects
- Python objects are represented as instances of a "python"
class in PHP
|
25
|
- Python objects are creating using the Python() object constructor
|
26
|
- Python objects work like PHP objects
|
27
|
|
28
|
- Once created, Python objects are stored in the engine symbol hash
|
29
|
- Python objects are retrieved by their handle
|
30
|
- If the method name is 'python':
- Import the requested module
- Construct a new Python object
- Register and return the new object
- Else:
- Retrieve the Python object handle
- Look for the requested method
- Call the method with any arguments
- Convert and return the result
|
31
|
- PHP converts all function and method calls to lowercase internally
- You type: $test->GetSomeValue()
- PHP sees: $test->getsomevalue()
- Python is case-sensitive, making it impossible to call any function or
method with capital letters from PHP!
|
32
|
- Build a map of Python object methods!
|
33
|
- Both "get" and "set" operations call the same
attribute handler
- Retrieve the requested Python object
- Find the named attribute
- Convert and return its value
- Note: No case-sensitivity hacks
necessary here!
|
34
|
- Allows access to the PHP environment from within the embedded Python
environment
- Functionality is still very limited!
|
35
|
|
36
|
|
37
|
- Extending Python objects from PHP
- Exposing PHP objects to Python
- More namespace sharing
- Global and local variables
- Multiple Python interpreters
- Better threading support
- Fix bugs
|
38
|
|
39
|
- Presentation Slides
- http://www.csh.rit.edu/~jon/pres/
- Python in PHP
- http://www.csh.rit.edu/~jon/projects/pip/
- Python
- http://www.python.org/
|