Skip to main content

SplMaxHeap aka SelfSort-“array”

class MyListHeap extends SplMaxHeap {
  public function compare( $value1, $value2 ) {
    return ( $value1['created_at'] - $value2['created_at'] );
  }
}

$list = new MyListHeap;

$list->insert(array('user_id' => 1, 'created_at' => time()-2000));
$list->insert(array('user_id' => 2, 'created_at' => time()-1000));
$list->insert(array('user_id' => 3, 'created_at' => time()-5000));

foreach($list as $l) {
  echo "{$l['user_id']}:{$l['created_at']}\n";
}

And the output will be:

2:1309908000
1:1309907000
3:1309904000

And the output will be:

2:1309908000
1:1309907000
3:1309904000

Read more: http://hu2.php.net/splmaxheap

Related Posts:

  • No Related Posts

How to extend the ArrayObject and create a custom array

to my friend with lots of love

class MyArray extends ArrayObject {
  public function __construct($array = array()){ 
    parent::__construct($array, ArrayObject::ARRAY_AS_PROPS);
  }

  public function get_property_string($sep = ':') {
    return implode($sep, $this->getArrayCopy());
  }

  public function __ToString() {
    return 'Array';
  }

  /* some custom method */
}

$rights = new MyArray;
$rights->append('LOGIN');
$rights->append('BETA_ACCESS');
$rights->append('WRITE_COMMENT');

var_dump($rights->get_property_string());

And the output will be:

string(31) "LOGIN:BETA_ACCESS:WRITE_COMMENT

Read more: http://php.net/manual/en/class.arrayobject.php

Related Posts:

  • No Related Posts

MySQL + MongoDB + PHP

We used MySQL with PHP… In the past months we try to migrate MySQL tables to MongoDB. And yes it was not a simple method ^_^ (but we did it).

Now I’ll tell you a simple source of bugs because i have too much fun with this. We have a users table with some fields for example member_type (int). The types already migrated to MongoDB. We fetch the user and we make a query with member_type value but it doesn’t work for us. Why?

Yes. When i call var_dump then i get a string but in MongoDB we need an integer. If you don’t use any ORM then if you fetch any data from MySQL what is an integer then you need a type conversion: (int)$row->member_type;

Related Posts:

Notice: Can I create `New` function in a class in PHP?

class wsGallery extends wsCore {
  const _PRIVATE = 1;
  const _PUBLIC  = 2;
  const _HIDDEN  = 3;
  const _SYSTEM  = 4;
  const _PROFILE = 5;

  private function __construct($gid) {
    parent::__construct();
    $this->gallery = wsCache::galleries()->findOne(array(
      '_id' => new MongoId($gid)
    ));
    $this->full_images = wsCache::images()->find(
      array('gallery_id' => $gid)
    );
  }
  static function new($name, $type) {
    $gallery = array(
      'name'     => $name,
      '_type'    => $type,
      'owner_id' => USER_ID,
      'images'   => array(),
      'requests' => array(),
      'accepted' => array()
    );
    wsCache::galleries()->insert($gallery);

    return new self($gallery['_id']);

  }
}

wsGallery::new('My gallery', wsGallery::_PRIVATE);

If you want to do this (or similar)… Forget the `new` keyword:

class wsGallery extends wsCore {
  const _PRIVATE = 1;
  const _PUBLIC  = 2;
  const _HIDDEN  = 3;
  const _SYSTEM  = 4;
  const _PROFILE = 5;

  private function __construct($gid) {
    parent::__construct();
    $this->gallery = wsCache::galleries()->findOne(array(
      '_id' => new MongoId($gid)
    ));
    $this->full_images = wsCache::images()->find(
      array('gallery_id' => $gid)
    );
  }
  static function create($name, $type) {
    $gallery = array(
      'name'     => $name,
      '_type'    => $type,
      'owner_id' => USER_ID,
      'images'   => array(),
      'requests' => array(),
      'accepted' => array()
    );
    wsCache::galleries()->insert($gallery);

    return new self($gallery['_id']);
  }
}

wsGallery::create('My gallery', wsGallery::_PRIVATE);

It’s another stupidity of PHP… the new keyword is reserved for instances [eg: new wsGallery()] But why? The parser can’t understand the Regular Expressions? `new Classname()` is not equal with `Classname::new()`.

The first is:
/\bnew\b [a-zA-Z_][a-zA-Z0-9_]*/

and the second is:
/\b[a-zA-Z_][a-zA-Z0-9_]*::[a-zA-Z_][a-zA-Z0-9_]*/

(or similar but the formats aren’t the same… Really not)

Related Posts:

Notice: “PRIVATE” const in PHP? wrong!

I need some const for GalleryTypes:

class wsGallery extends wsCore {
  const QUEUE     = 'requests';
  const ACCEPTED  = 'accepted';

  // This is the problem
  const PRIVATE = 1;
  const PUBLIC  = 2;
  const HIDDEN  = 3;
  const SYSTEM  = 4;
  const PROFILE = 5;
  // end

  // Construct
  public function __construct($gid) {
    parent::__construct();
  }
  $this->gallery = wsCache::galleries()->findOne(
    array('_id' => new MongoId($gid))
  );
  $this->full_images = wsCache::images()->find(
    array('gallery_id' => $gid)
  );
  // ...
}

It’s WRONG!

class wsGallery extends wsCore {
  const QUEUE     = 'requests';
  const ACCEPTED  = 'accepted';

  // This is the problem
  const _PRIVATE = 1;
  const _PUBLIC  = 2;
  const _HIDDEN  = 3;
  const _SYSTEM  = 4;
  const _PROFILE = 5;
  // end

  // Construct
  public function __construct($gid) {
    parent::__construct();
    $this->gallery = wsCache::galleries()->findOne(
      array('_id' => new MongoId($gid))
    );
    $this->full_images = wsCache::images()->find(
      array('gallery_id' => $gid)
    );
  }
  // ...
}

It’s OK… But ugly… PHP is very stupid.

If i want to get a speaking name my fist idea is:

wsGallery::PRIVATE
wsGallery::PUBLIC
wsGallery::PROTECTED

But it’s not free because it’s a locked name (for: private function asdasd() {} ) and i need to call in similar formats:

wsGallery::_PRIVATE
wsGallery::PRIVATE_GALLERY
wsGallery::PRIVATE_TYPE

I think i’s ugly and I can not understand it… WHY the PHP Parser is so bad?

Related Posts: