[PHP-DEV] Optional parameter a la exec()
Hello, A few questions :) 1) I'm trying to get an optional parameter working like the output array on exec(). That is to say, I'd like an optional parameter that doesn't need to be explicitly passed in by reference, but if it's present I want to populate it with a long value. This *should* be simple, but I can't seem to make it work without seg faulting all over :P My code looks something like this at the moment, where 'type' is the variable I'd like to fill in with a long value: ---snip--- char *foo = NULL; zval **type = NULL; int foo_len; if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, s|/z, foo, foo_len, type) == FAILURE) { WRONG_PARAM_COUNT; } At this point, what code would I add if I just wanted to always set type to '15' and return from the function without doing anything else? 2) As I understand it, adding a bang (!) after the '/z' in the parameter string would mean that the parameter could be passed in with a null? What does this look like in PHP code, as I've never actually done it? Something like my_function(some string, null)? And how do I test for it in my module code? Just see if (in the above code snippet) 'type == NULL'? I ask because there will be another parameter after the long 'type' in the real function, an optional boolean value. 3) this is probably a REALLY stupid question, but I guess I'm a little uneasy about passing in char*'s zend_parse_parameters[_ex]() and not cleaning up with efree -- I assume that if I tacked on a simple 'return;' to the code snippet above, that's not going to be a memory leak or something? zend_parse_parameters[_ex]() is doing something to make this not happen? Looking through the existing modules, I notice no one else worrying, but I can't help it -- I need a word of reassurance here :) Thanks for any help! Wood Shavings! Andrew Patterson -- PHP Development Mailing List http://www.php.net/ To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] Calling other PHP functions from your extension
Well have you tried it? No :) I just got your email last night and responded, I intended to try this morning. Use the same example, and instead of putting test_function, put phpinfo, or any php function that doesn't take parameters, and see the result, it works! I'll do that right now...yup, worked, thanks! To pass parameters to the function you want to call, it's a bit more complicated. Take a look at the call_user_function declaration in zend_API.h, you will see that there is one field called parameter_count (that's easy to understand what this is for), and the next field is params. Those are zvals, so it's easy, put the parameters in this array as zvals, set the correct parameter count and bingo. Note that the documentation is WRONG. The call to call_user_function_ex is incorrect, it doesn't have the right number of parameters. For your case, you'd better go with call_user_function for now, not the ex one. Also, it should read TSRMLS_FETCH, not TSRMSLS_FETCH (don't forget this macro or you will loose the thread safe context). And thanks for the additional head's up! I had to go read the zend api headers to get it working -- I think you're rigyt, call_user_function() is sufficient for me in this case. I just got phpinfo() working, I'll try getting eval() going in a few minutes. Thanks again for the help? Wood Shavings! Andrew Patterson -- PHP Development Mailing List http://www.php.net/ To unsubscribe, visit: http://www.php.net/unsub.php
[PHP-DEV] Calling other PHP functions from your extension
Hello, I've been messing around with my own PHP extension for a couple of days now (I was pleasantly surprised to find how easy it was to write one!), one I've been meaning to write for over a year and I had a couple of questions I hope I can get some help on: i) is it possible to call other functions in PHP from your functions? In particular, I'd like to eval() a string I have and return the resultant string to the user -- is there anyway I can do this? ii) I'm making fast progress in my extension, and I think it might be something that might be worthy of inclusion into the main PHP distribution. What steps are involved in submitting a module for such consideration? Wood Shavings! Andrew Patterson -- PHP Development Mailing List http://www.php.net/ To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] Calling other PHP functions from your extension
Take a look at http://www.php.net/manual/en/zend.calling-user-functions.php I guess that's what you need. Hmmm. I'm not sure, but I thought this was about calling user-defined functions, not existing PHP module functions. Looking at my last email, I probably wasn't clear enough :P I don't want to call a passed in function name a la a callback; I want to call a core PHP function like implode(), or explode() or mysql_connect() (for example) -- specifically, I need to call the eval() function. Can you do that in the manner defined for calling user functions? Are the functions explode, implode, etc. in the user function table? I thought those just contained the functions that had been defined by the user in the currently being parsed script -- not all functions defined by loaded modules. Am I wrong? What is your extension about? I'm the author/maintainer of a pair of PHP libraries named the NDBE the NObjects (http://ndbe.sourceforge.net); the former is a database editor/ abstact layer library, and the latter is general 'odds and ends' library of scripts, some of which the former requires to run. One such script emulates the Windows registry, providing a persistant, filesystem-like hive/key/value system for storing information in various formats. The scripts provides an object (NHive) that allows you to access this 'registry', which is actually stored on the drive in a file-tree (at a location you specify). Furthermore, we expand on the idea of the registry to add in the concept of 'ethereal hives'. If you're familliar with the Windows registry, picture them as sort of a read-only hive (like HKEY_LOCAL_USER) which is only used if the requested key/value can't be found in the *actual* hive (of the same name). Ethereal hives sort of provide a set of default values; the first time you ask for 'HIVE/foobar/foo', and it can't find it in the *real* registry, it looks it up in the ethereal hive of the same name ('HIVE' in this case). If it finds a key 'foobar' with a value 'foo', it would return that value contained in 'foo'. If you write out a new value to 'HIVE/foobar/foo' it will write it to the REAL registry however; and ever after when it ask for that key ('HIVE/foobar/foo') it will give you the value from the real registry, which you just created and set. It's a little confusing to explain, but solves a wide variety of problems we had with default values, and allows you to do wonderful things with checking them in. In short, it can -- for the most part -- allow you to do away with configuration files. It works very well, and we (my partners I) have found it *very* useful over the last couple of years, and most of the NObjects and all of the NDBE runs on it. We had the natural idea of making it into a PHP module more than a year ago, but none of us could find the time -- I finally did a few days ago and was surpised how quickly it's come along. Of course, all of the logic is more or less written out for me; it's in the NHive.php script :) It's just a matter or converting the code to a somewhat lower-level language (namely C) and making it properly-cross platform. I intend to make the Win32 version use the *actual* Windows registry, but that's for later -- haven't ever actually *used* PHP on Windows before. Anyways, that's the (long-winded) explanation. I've got 95% of the utility functions written, I just have to do the set/get/create/delete functions. After that, I'll be thoroughly testing it (fortunately I have a complete library and application in the NDBE to test it on) and then be submitting it wherever it should be submitted for consideration -- assuming someone's interested. Otherwise, I guess it'll just be posted on sourceforge or freshmeat :) Wood Shavings! Andrew Patterson -- PHP Development Mailing List http://www.php.net/ To unsubscribe, visit: http://www.php.net/unsub.php