Skip to main content

Main navigation

  • Solutions
    • Zend Server
    • ZendPHP Enterprise
    • Laminas Enterprise Support
  • Services
    • Services
      • Services Overview
    • Services Links
      • Services Links Row
        • Migrations
        • PHP Long-Term Support
        • Custom Consulting
        • Audits
        • CI/CD
  • Training
    • Training Featured Links
      • Training
    • Training Links
      • Training Links Row
        • PHP Training
        • Zend Framework Training
        • IBM i Training Exercises
  • Resources
    • Resources Featured Links
      • Resources
    • Resource Links
      • Resource Links Row
        • Papers & Videos
        • Events & Webinars
        • Recorded Webinars
        • Blog
        • Case Studies
        • PHP Security Center
  • Support
    • Support Featured Links
      • Support
      • Request Support
    • Support Links
      • Support Links Row
        • PHP Long-Term Support
        • Knowledgebase
        • Documentation
        • Download Software
        • Download Plugins
  • Free Trial

Secondary Navigation

  • PHP Security Center
  • Blog
  • Store
  • Downloads
    • Downloads
    • Plugins
    • MyZend Account
  • Company
    • About Zend by Perforce
    • Careers at Perforce
    • Customers
    • Partners
    • Press
  • Contact
    • Contact Us
    • Request Support
    • Subscribe
Created with Avocode.

Secondary Navigation

  • PHP Security Center
  • Blog
  • Store
  • Downloads
    • Downloads
    • Plugins
    • MyZend Account
  • Company
    • About Zend by Perforce
    • Careers at Perforce
    • Customers
    • Partners
    • Press
  • Contact
    • Contact Us
    • Request Support
    • Subscribe
Home
Zend

Main Navigation - Mega Menu

  • Products

    Main Navigation - Mega Menu

    • ZendPHP
      PHP Runtime and Support
    • PHP LTS
      Patches for EOL PHP
    • Zend Server
      PHP Application Server
    • Laminas Enterprise Support
      Formerly Zend Framework
  • Services

    Main Navigation - Mega Menu

    • Service Overview
    • Migration Services
    • Audits
    • CI/CD
    • Custom Consulting

    Services

    Innovate faster and cut risk with PHP experts from Zend Services.

    Explore Services

  • Training

    Main Navigation - Mega Menu

    • Training Overview
    • IBM i Training Exercises

    Training

    Learn PHP from PHP experts with free, on-demand, and instructor led courses.

    Explore Training

  • Resources

    Main Navigation - Mega Menu

    • Explore Resources
    • Events & Webinars
    • Papers & Videos
    • Recorded Webinars
    • Blog
    Cloud Orchestration

    Orchestrating Your PHP Applications

    Watch Now
  • Support

    Main Navigation - Mega Menu

    • Explore Support
    • PHP Long-Term Support
    • Knowledgebase
    • Documentation
    • Download Software
    • Download Plugins
    • Request Support

    Support

    Submit support requests and browse self-service resources.

    Explore Support

  • Try Free
  • PHP Security Center
  • Blog
  • Store
  • Downloads

    Main Navigation - Mega Menu

    • Downloads
    • Plugins
    • MyZend Account
    • Downloads
    • Plugins
    • MyZend Account
  • Company

    Main Navigation - Mega Menu

    • About Zend by Perforce
    • Careers at Perforce
    • Customers
    • Partners
    • Press
    • About Zend by Perforce
    • Careers at Perforce
    • Customers
    • Partners
    • Press
  • Contact

    Main Navigation - Mega Menu

    • Contact Us
    • Request Support
    • Subscribe

TECHNICAL GUIDE

Writing PHP Extensions

Request PDF Version
Writing PHP Extensions
1. Setting up Your PHP Build Environment on Linux
2. Generating a PHP Extension Skeleton
3. Building and Installing a PHP Extension
4. Rebuilding Extensions for Production
5. Extension Skeleton File Content
6. Running PHP Extension Tests
7. Adding New Functionality
8. Basic PHP Structures
9. PHP Arrays
10. Catching Memory Leaks
11. PHP Memory Management
12. PHP References
13. Copy on Write
14. PHP Classes and Objects
15. Using OOP in our Example Extension
16. Embedding C Data into PHP Objects
17. Overriding Object Handlers
18. Answers to Common Extension Questions

17. Overriding Object Handlers

If you remember, each object keeps an object handlers table. We started to use it in the previous chapter on embedding C data. 

There are many different handlers, and all of them can be overridden to change object behavior. For example: 

  • ArrayObject overrides read/write/has/unset_dimension handlers to make an object behave as an array. 
  • ext/simplexml allows traversing XML tree through both property and dimension handlers. 
  • ext/ffi calls native functions through get_method handler etc. 

We will override get_debug_info handler to make var_dump() print the value of our C factor field. This method takes the “object” argument and returns a HashTable with properties to zvals that are going to be displayed. The default implementation returns a real PHP properties table, but we don’t have any PHP properties. Instead, we construct a new one and add the value of C factor, using the magic name “{factor}”. We also specify that this is not the real properties table, but a temporary table, that should be freed after printing. 

static HashTable* scaler_get_debug_info(zval *object, int *is_temp)
{
    scaler_t *scaler = OBJ_SCALER(object);
    HashTable *ret = zend_new_array(1);
    zval tmp;

    ZVAL_LONG(&tmp, scaler->factor);
    zend_hash_str_add(ret, “{factor}”, sizeof(“{factor}”)-1, &tmp);
    *is_temp = 1;
    return ret;
}

Of course, we have to override the default value of get_debug_info handler, of Scaler class, by our own. 

    memcpy(&scaler_object_handlers, &std_object_handlers,
        sizeof(zend_object_handlers));
    scaler_object_handlers.offset = XtOffsetOf(scaler_t, std);
    scaler_object_handlers.get_debug_info = scaler_get_debug_info;
    zend_declare_class_constant_long(scaler_class_entry,
        “DEFAULT_FACTOR”, sizeof(“DEFAULT_FACTOR”)-1, DEFAULT_SCALE_FACTOR);

Works without problems: 

$ php -r ‘$o = new Scaler(4); $x = 5; $o->scale($x); var_dump($x,$o);’
int(20)
object(Scaler)#1 (1) {
    [“{factor}”]=>
    int(4)
}

Request PDF Version

Book traversal links for 17. Overriding Object Handlers

  • ‹ 16. Embedding C Data into PHP Objects
  • Writing PHP Extensions
  • 18. Answers to Common Extension Questions ›

Footer menu

  • Products
    • ZendPHP
    • PHP Long-Term Support
    • Zend Server
    • Laminas Enterprise Support
    • PHP Development Tools
  • Resources
    • PHP Security Center
    • Papers & Videos
    • Events & Webinars
    • Recorded Webinars
    • Blog
    • Case Studies
  • Services
    • PHP Long-Term Support
    • Migration Services
    • Audits
    • CI/CD Services
    • Custom Consulting
  • Downloads
    • MyZend Account
    • Plugins
    • Container Registry
  • Training
    • IBM i Training Exercises
  • Support
    • PHP Long-term Support
    • Knowledgebase
    • Documentation
  • Store
  • Hubs
    • PHP Versions Guide
    • Developing Web Apps With PHP
    • Guide to PHP and IBM i
  • FREE TRIALS
    • ZendPHP
    • Zend Server
  • Company
    • About Zend by Perforce
    • Careers at Perforce
    • Customers
    • Partners
    • Press
  • Contact
    • Request Support
    • Subscribe
Home

Zend by Perforce © Perforce Software, Inc.
Terms of Use  |  Privacy Policy | Sitemap

Social Menu

  • Facebook
  • LinkedIn
  • Twitter
  • YouTube
  • RSS
Send Feedback