[PHP] distinguish between null variable and unset variable
How can I tell the difference between a variable whose value is null and a variable which is not set? // cannot use === null: ket% php -r '$null = null; var_dump(null === $null);' bool(true) ket% php -r 'var_dump(null === $unset);' bool(true) ket% // - cannot use isset() either: ket% php -r '$null = null; var_dump(isset($null));' bool(false) ket% php -r 'var_dump(isset($unset));' bool(false) ket% -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] get file from object
How do I get the file where a class is defined, from an instance of that class? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: runtime access to static variable
this does beg the question why don't you know the classname at runtime.. seems to be a slight design flaw and may make sense for you to post the full problem (you must have chosen to implement this for a reason..) The full problem is: I started off with a DeployTask for deploying a new instance of my web project to DreamHost: http://cgi.sfu.ca/~jdbates/tmp/php/200812170/DeployTask.class.phps (It is a task in the symfony framework, but basically the DeployTask::execute() method gets called) The task takes about thirty minutes to run, so I broke it up into steps. After each step, it updates a database row so that a user who started the task through a web interface can monitor its progress. Great so far. Now I decided to create an UpdateTask for updating deployed instances to the latest version of my code: http://cgi.sfu.ca/~jdbates/tmp/php/200812170/UpdateTask.class.phps Of course it also takes a long time to run, so my first iteration just extends the DeployTask and defines different steps. UpdateTask inherits DeployTask::execute(), which drives the steps and updates the database. Unfortunately, in the inherited DeployTask::execute(), self::$STEPS does not refer to UpdateTask::$STEPS, it refers to DeployTask::$STEPS : ( I gather with late static bindings in PHP 5.3, static::$STEPS does what I want? Anyway, DeployTask::execute() is not static, so I know whether $this is an instance of DeployTask of UpdateTask: get_class($this) returns it. Which brought me to my contrived example: ket% cat test.php ?php class Test { public static $STEPS = array( 'foo', 'bar'); } $className = 'Test'; var_dump($className::$STEPS); ket% Except that in real life, $className = get_class($this) Check this out: http://us2.php.net/manual/en/language.oop5.static.php It actually won't work until 5.3.0 when they add late static binding. I ran my contrived example with PHP 5.3alpha3, and it worked! $className::$STEPS is not a parse error in 5.3, which is cool : ) Except that in 5.3, I will probably just use static::$STEPS anyway : P In the meantime, I will probably use reflection as suggested: // HACK: Use static::$STEPS in PHP 5.3: // http://php.net/oop5.late-static-bindings $class = new ReflectionClass($this); $steps = $class-getStaticPropertyValue('STEPS'); -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] runtime access to static variable
How do I access a static variable when I do not know the name of the class until runtime? I have the following example PHP: ket% cat test.php ?php class Test { public static $STEPS = array( 'foo', 'bar'); } $className = 'Test'; var_dump($className::$STEPS); ket% Unfortunately when I run it I get: ket% php test.php Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM in /home/jablko/trash/test.php on line 13 ket% I can call a static function using call_user_func(array($className, 'functionName')), and I can access a class constant using constant($className.'::CONSTANT_NAME'). How do I access a static variable? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] new $foo-className(); Class name must be a valid object or a string
I am trying to load PHP objects stored in a database, where the class name is stored in a column: $object = new $resultSet-getString(1); This fails for the same reason that the following fails: ?php class Foo { public function className() { return 'Foo'; } } $foo = new Foo; $bar = new $foo-className(); Fatal error: Class name must be a valid object or a string in test.php on line 12 I guess this error is due to the confusion of parsing () as the argument list for the className function, or the Foo constructor... I work around this error by using a temp variable: $tmp = $foo-className(); $bar = new $tmp; - however the above reads like hacky code : ( When calling dynamically named functions, I generally use call_user_func() to avoid awkwardness with $object-$tmp($arg1, ...) In other words, I prefer: call_user_func(array($object, 'get'.$someName), $arg1, ...); - to: $tmp = 'get'.$someName; $object-$tmp($arg1, ...); However there does not appear to be an analog of call_user_func() for constructing new instances of dynamically named classes? If I recall correctly, there was also a way to work around calling dynamically named functions (e.g. $object-$tmp($arg1, ...);) using curly braces: $object-{'get'.$someName}($arg1, ...); - however I cannot recall the exact syntax. Can anyone confirm that there is a curly brace syntax for calling dynamically named functions? Could it be applied to instantiating dynamically named classes? Can anyone recommend a cleaner alternative to: $tmp = $foo-className(); $bar = new $tmp; Thanks and best wishes, Jack -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] redirect stdout to stderr
How can I implement in PHP, a script which redirects stdout to stderr, such that echo, etc. print to stderr instead of stdout? I can redirect stdout to stderr when invoking PHP like so: php script-name 2 However I want to perform this redirection within the script itself. The solution I currently use is output buffering: ob_start(); // Call library code fwrite(STDERR, ob_get_contents()); ob_end_clean(); However I wonder if there's a more efficient way, so that output appears on stderr immediately, rather than waiting for fwrite(STDERR, ob_get_contents()); My reason for wanting this is to create a Subversion pre-commit hook using PHP_CodeSniffer: http://pear.php.net/package/PHP_CodeSniffer I want: 1) Commits to our Subversion repository to be checked against our coding standard with PHP_CodeSniffer 2) Commits to fail when PHP_CodeSniffer returns an error 3) PHP_CodeSniffer's report to be displayed to the Subversion user, so they can fix any problems I achieved 1) and 2), but PHP_CodeSniffer prints its report to stdout and Subversion only displays stderr to the user, not stdout. So to make this pre-commit hook fool proof, I want it to redirect PHP_CodeSniffer's report to stderr. Anyone have better suggestions than output buffering? Much thanks, Jack signature.asc Description: This is a digitally signed message part
[PHP] [pcre] backreferences to all matches of a repeated subexpression
I'm trying to pull all the components out of strings structured like: word followed by any number of ( dot word or square bracketed string ) This is an example: foo.bar[ab.cd].baz From the above example, I want: array('foo', 'bar', 'ab.cd', 'baz'); A regular expression to match these strings, including parenthesis around the parts I want to pull out: preg_match('/(^\w+)(?:\.(\w+)|\[([^]]+)\])*/', $subject, $matches); However applied to the above example: $matches[1] = 'foo'; // First subexpression $matches[2] = 'baz'; // Last value of second subexpression $matches[3] = 'ab.cd'; // Last value of third subexpression I'm not sure how to get an array of all subexpression matches, in the order matches appear in the subject, instead of the order expressions appear in the pattern. I think I might be able to do something using the Perl (?{code}) construction, e.g. ...(?:\.(\w+)(?{$parts[] = \2})|\[([^]]+)(?{$parts[] = \3})\])*... but I haven't thought about it too much because PHP doesn't support this construction. Any ideas much appreciated. Thanks, Jack -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php