Wednesday, April 27, 2011

PHP (and vs. &&)

PHP has two methods which should work the same way. The `and` operator and the `&&` operator. But if you do not pay attention your program will be broken.
<?php
function has_access($type, $id) {
  if ($id === 0) return true;
  else return false;
}

$article_id = 0;
$uid = 14;

$is_zero = $article_id == 0;
$has_acc = has_access('update', $uid);
$ok      = $article_id == 0 && has_access('update', $uid);
$wtf     = $article_id == 0 and has_access('update', $uid);

echo "\$article_id == 0                                  // => ";
var_dump($is_zero);
echo "has_access('update', \$uid)                        // => ";
var_dump($has_acc);
echo "\n";
echo "\$article_id == 0 && has_access('update', \$uid)    // => ";
var_dump($ok);
echo "\$article_id == 0 and has_access('update', \$uid)   // => ";
var_dump($wtf);
The optimal output:
$article_id == 0                                  // => bool(true)
has_access('update', $uid)                        // => bool(false)

$article_id == 0 && has_access('update', $uid)    // => bool(false)
$article_id == 0 and has_access('update', $uid)   // => bool(false)
The real output:
$article_id == 0                                  // => bool(true)
has_access('update', $uid)                        // => bool(false)

$article_id == 0 && has_access('update', $uid)    // => bool(false)
$article_id == 0 and has_access('update', $uid)   // => bool(true)
Or the simple question:
$variable = true and false;
What will be the value of $variable? Just check it with `php -a`
I think it's a very stupid operation. (see: php.net > Operator Precedence)

Tuesday, April 26, 2011

A simple Sass example

Here is a Sass example. Sass is a good productivity booster or just a passing folly.

We need a border. It's simple:

.edit_container {
  border: 1px solid #a77;
}

.editable_div {
  border: 1px solid black;
}

.gallery_preview {
  border-top: 1px solid black;
  border-bottom: 1px solid black;
}

.gallery_preview_big {
  border-bottom: 1px solid #555;
}

It's just 4 simple class definition. Where is the DRY rule? Nowhere.

Remake it with Sass:

@mixin border($width: 1px, $style: solid, $color: black)
  border: $width $style $color

.edit_container
  @include border($color: #a77)

.editable_div
  @include border()

.gallery_preview
  @include border()
  border-left: 0
  border-right: 0

.gallery_preview_big
  border-bottom: 1px solid #555

Yes... That's not very nice (.gallery_preview, .gallery_preview_big).

@mixin border( $width: 1px, $style: solid, $color: black, $top: true, $bottom: true, $left: true, $right: true )
  @if $top
    border-top: $width $style $color
  @if $left
    border-left: $width $style $color
  @if $right
    border-right: $width $style $color
  @if $bottom
    border-bottom: $width $style $color
}

.edit_container
  @include border($color: #a77)

.editable_div
  @include border()

.gallery_preview
  @include border($left: false, $right: false)

.gallery_preview_big
  @include border($left: false, $right: false, $top: false)

This is more beautiful but it's not follow the DRY rule. Use: @mixin, @if, @each and lists with nth().

@mixin _base_border($prefix, $width, $style, $color)
  border-#{$prefix}: $width $style $color

@mixin border( $width: 1px, $style: solid, $color: black, $top: true, $bottom: true, $left: true, $right: true )
  @each $edge in ($top, top), ($left, left), ($right, right), ($bottom, bottom)
    @if nth($edge, 1)
      @include _base_border( nth($edge, 2), $width, $style, $color)

.edit_container
  @include border($color: #a77)

.editable_div
  @include border()

.gallery_preview
  @include border($left: false, $right: false)

.gallery_preview_big
  @include border($left: false, $right: false, $top: false)

It's ok ^_^ We made it. What is the output?

.edit_container {
  border-top: 1px solid #a77;
  border-left: 1px solid #a77;
  border-right: 1px solid #a77;
  border-bottom: 1px solid #a77; }

.editable_div {
  border-top: 1px solid #000;
  border-left: 1px solid #000;
  border-right: 1px solid #000;
  border-bottom: 1px solid #000; }

.gallery_preview {
  border-top: 1px solid #000;
  border-bottom: 1px solid #000; }

.gallery_preview_big {
  border-bottom: 1px solid #000; }

With command: sass --style compressed example.sass

.edit_container{border-top:1px solid #a77;border-left:1px solid #a77;border-right:1px solid #a77;border-bottom:1px solid #a77}.editable_div{border-top:1px solid #000;border-left:1px solid #000;border-right:1px solid #000;border-bottom:1px solid #000}.gallery_preview{border-top:1px solid #000;border-bottom:1px solid #000}.gallery_preview_big{border-bottom:1px solid #000}

If you don't like the syntax of Sass just use Scss:

@mixin _base_border($prefix, $width, $style, $color) {
  border-#{$prefix}: $width $style $color;
}

@mixin border( $width: 1px, $style: solid, $color: black, $top: true, $bottom: true, $left: true, $right: true ) {
  @each $edge in ($top, top), ($left, left), ($right, right), ($bottom, bottom) {
    @if nth($edge, 1) {
      @include _base_border( nth($edge, 2), $width, $style, $color);
    }
  }
}

Monday, April 25, 2011

What's cool in Haml/Sass 3.1 ?

Haml and Sass 3.1 released. You  can install them now with two separated gems:

gem install haml
gem install sass

The biggest change between Haml 3.0 and Haml 3.1 by far is the removal of Sass [from v3.0.22]. (more info in the Haml changelog)

Sass has lots of exciting changes. (more info in the Sass changelog)
Let's look at what's new in Sass.

Functions (user defined)

$grid-width: 40px;
$gutter-width: 10px;

@function grid-width($n) {
  @return $n * $grid-width + ($n - 1) * $gutter-width;
}

We can declare a normal function what is not just a property group with parameter. Pretty cool.

This is a function named grid-width what can calculate the width of grid cells. The result of the function is marked with @return. You can use some other functions like @if and @for.

Keyword arguments

Mixins and functions now support keyword arguments. Instead of memorizing the order of arguments for each mixin/function, you can pass the arguments in any order by naming them. Example:

@mixin border-radius($value, $moz: true, $webkit: true, $css3: true) {
  @if $moz { -moz-border-radius: $value }
  @if $webkit { -webkit-border-radius: $value }
  @if $css3 { border-radius: $value }
}

@include border-radius(10px, $webkit: false)

This sets the $webkit argument to false while leaving the other arguments to their default values.

@each (my personal favorite)

Sass also adds the @each directive which assigns a variable to each item in a list in turn just like @for does for numbers. Example:

@each $menu_type in text, video, quote, image {
  .blog-#{$menu_type} {
    background-image: url('../images/blog/type_#{$menu_type}.png');
  }
}

Compiled to

  blog-text {
    background-image: url('../images/blog/type_text.png');
  }
  blog-video {
    background-image: url('../images/blog/type_video.png');
  }
  blog-quote {
    background-image: url('../images/blog/type_quote.png');
  }
  blog-image {
    background-image: url('../images/blog/type_image.png');
  }

On Tuesday when I go to work I make some test about it and try to migrate our PHP and Ruby systems.

Notify: If you have an installed Haml (old version) and execute the `gem update` command then don't forget the `gem install sass` because it will not be installed (remains the old version)...

Saturday, April 23, 2011

prettify into Tumblr

Open your own Tumblr dashboard and here click to the Customize.
In the Theme menu click to Custom HTML... It will load code of the current template... Find the `<body>` string and replace it with this lines:

<body onload="prettyPrint()">

Then insert these lines befor ``:

  <link rel="stylesheet" type="text/css" href="http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.css"></link>  
<script src="http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.js"></script>
<script src="http://google-code-prettify.googlecode.com/svn/trunk/src/lang-css.js"></script>
<script src="http://google-code-prettify.googlecode.com/svn/trunk/src/lang-sql.js"></script>
<script src="http://google-code-prettify.googlecode.com/svn/trunk/src/lang-yaml.js"></script>
<script src="http://google-code-prettify.googlecode.com/svn/trunk/src/lang-hs.js"></script>

ok... the installation is ready. (see http://google-code-prettify.googlecode.com/svn/trunk/src/ for other additional supported languages)

To modify default style click the Advanced menu and write your own style under the `Add custom CSS` label. Eg:

pre .str, code .str { color: #65B042 !important; }
pre .kwd, code .kwd { color: #E28964 !important; }
pre .com, code .com { color: #AEAEAE !important; font-style: italic !important; }
pre .typ, code .typ { color: #89bdff !important; }
pre .lit, code .lit { color: #3387CC !important; }
pre .pun, code .pun { color: #fff !important; }
pre .pln, code .pln { color: #fff !important; }
pre .tag, code .tag { color: #89bdff !important; }
pre .atn, code .atn { color: #bdb76b !important; }
pre .atv, code .atv { color: #65B042 !important; }
pre .dec, code .dec { color: #3387CC !important; }

pre.prettyprint, code.prettyprint {
background-color: #000 !important;
-moz-border-radius: 4px !important;
-webkit-border-radius: 4px !important;
-o-border-radius: 4px !important;
-ms-border-radius: 4px !important;
-khtml-border-radius: 4px !important;
border-radius: 4px !important;
}

pre.prettyprint {
width: 95% !important;
margin: 1em auto !important;
padding: 1em !important;
white-space: pre-wrap !important;
}

ol.linenums { margin-top: 0 !important; margin-bottom: 0 !important; color: #AEAEAE !important; }
li.L0,li.L1,li.L2,li.L3,li.L5,li.L6,li.L7,li.L8 { list-style-type: none !important }
li.L1,li.L3,li.L5,li.L7,li.L9 { }

pre { padding: 5px; font-size: 1em !important; }

hr { border: none; border-top: 1px dotted #AA2200; width: 80%; margin-left: 0; }

Now if you want insert any code snippet into your post you need to edit as HTML (right side of the editor icon set) and put it into a `pre` tag (or `code`). Eg:

<pre class='prettyprint lang-php'>
<?php echo 'Test' ?>
</pre>

Where lang-XXX is the language what you want to use.

If you have any questions feel free to ask.

Thursday, April 21, 2011

Re: JDN 2/11: The UK Approach to Unmanned Aircraft Systems

Lawyer: Admit that the above can be seen of code you wrote?
Programmer: Yes.
Lawyer: Admit that this code is buggy?
Programmer: No.
Lawyer: But it's visible that certain input values ​​pass control to the wrong place. So I ask again, admit mistakenness of the code?
Programmer: No... because under normal use this code is flawless.
Lawyer: What do you mean 'normal use'?
Programmer: Let's just say that not killing people.

4/21/11 1:40 PM

Wednesday, April 20, 2011

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)

Tuesday, April 19, 2011

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 not free because it's a locked name (for: private function asdasd() {} ) and i need to call in similar format:
wsGallery::_PRIVATE
wsGallery::PRIVATE_GALLERY
wsGallery::PRIVATE_TYPE

I think i's ugly and i can not understand it... WHY? So bad the PHP Parser?

prettify test

single-line.php:

<?= 'omg! It works!'; ?>

multi-line.php:

<?php
  // Yes?
  echo 'omg! It works!';
  /*
   * Multi-line comment
   * just a test not more
   */
?>

single-line.rb:

<%= 'omg! It works!'; %>

multi-line.rb:

<% 10.times do |x| %>
  omg! It works!
<% end %>