Skip to content

Instantly share code, notes, and snippets.

View nikic's full-sized avatar

Nikita Popov nikic

View GitHub Profile
<?php
class Test {
const A = Test::class;
const B = self::class;
const C = parent::class;
const D = static::class;
}
var_dump(Test::A);
/var/lib/gems/1.8/gems/maruku-0.6.0/lib/maruku/string_utils.rb:71:in `normalize_key_and_value': undefined method `strip' for nil:NilClass (NoMethodError)
from /var/lib/gems/1.8/gems/maruku-0.6.0/lib/maruku/string_utils.rb:59:in `parse_email_headers'
from /var/lib/gems/1.8/gems/maruku-0.6.0/lib/maruku/string_utils.rb:55:in `each'
from /var/lib/gems/1.8/gems/maruku-0.6.0/lib/maruku/string_utils.rb:55:in `parse_email_headers'
from /var/lib/gems/1.8/gems/maruku-0.6.0/lib/maruku/input/parse_doc.rb:29:in `parse_doc'
from /var/lib/gems/1.8/gems/maruku-0.6.0/lib/maruku/maruku.rb:30:in `initialize'
from /var/lib/gems/1.8/gems/jekyll-0.11.2/bin/../lib/jekyll/converters/markdown.rb:120:in `new'
from /var/lib/gems/1.8/gems/jekyll-0.11.2/bin/../lib/jekyll/converters/markdown.rb:120:in `convert'
from /var/lib/gems/1.8/gems/jekyll-0.11.2/bin/../lib/jekyll/convertible.rb:46:in `transform'
from /var/lib/gems/1.8/gems/jekyll-0.11.2/bin/../lib/jekyll/convertible.rb:84:in `do_layout'
string {
string capitalize()
string titleize()
string toLower()
string toUpper()
int length()
string slice(int $offset, int $length = magic)
string replaceSlice(string $replacement, int $offset, int $length = magic)
string {
string capitalize()
string titleize()
string toLower()
string toUpper()
int length()
string slice(int $offset, int $length = magic)
string replaceSlice(string $replacement, int $offset, int $length = magic)
@nikic
nikic / guestbook.markdown
Created July 29, 2012 14:21
Quick doesn't have to mean dirty: Also applies to PHP!

Quick doesn't have to mean dirty: Also applies to PHP!

This is just a quick response to http://me.veekun.com/blog/2012/07/28/quick-doesnt-mean-dirty/. I won't bother to write a proper blog post for this, so a Gist will have to do ;)

When I read that article, one thing really striked me: If you want to quickly create a web app in PHP, you do exactly the same. I mean, exactly.

I never used the Silex microframework before, so I took this as a chance to see how it works. I'll just do the same as eevee did, only with a bit less commentary (this is a Gist after all!)

I hope that this will show you that PHP and Python are really similar to work with. Also this should show that just because you're using PHP, doesn't mean that you write dirty code. The similarity in the process and code is really incredible :)

@nikic
nikic / coroutine.php
Created July 14, 2012 13:25
A coroutine example: Streaming XML parsing using xml_parser
<?php
error_reporting(E_ALL);
/* Data can be send to coroutines using `$coroutine->send($data)`. The sent data will then
* be the result of the `yield` expression. Thus it can be received using a code like
* `$data = yield;`.
*/
/* What we're building in this script is a coroutine-based streaming XML parser. The PHP
@nikic
nikic / µ.php
Created June 30, 2012 23:58
Something I found in my codebase...
<?php
class Registry implements ArrayAccess {
public $this = null;
protected $impls = array();
protected $curImpl = 0;
public function &__get($key) {
if (isset($this->__onUndefinedProperty)) {
Fix some lengths in crypt()
Use salt_len_in instead of strlen(salt) or PHP_MAX_SALT_LEN, otherwise too
much memory will be allocated.
sha512 has a 86 character checksum, not 43. That probably was a copy&paste
from the sha256 code which indeed has 43.
The allocation also were using sizeof(char *) instead of sizeof(char), thus
allocating 4 or 8 times as much memory as necessary.
@nikic
nikic / microbench.php
Created June 22, 2012 23:42
Microbenchmark of generator implementation
<?php
error_reporting(E_ALL);
function xrange($start, $end, $step = 1) {
for ($i = $start; $i < $end; $i += $step) {
yield $i;
}
}
<?php
register_tick_function(function() {
static $i = 0;
if (++$i >= 100000) {
throw new Exception('Too many ticks!');
}
});
declare(ticks=1) {