Dimitry Zolotaryov
Posts
Perl has a neat way to define both a getter and a setter in the same class method. The basic definition goes a little like this:
sub color { my $shift; if (@_) { # are there any more parameters? # yes, it's a setter: $self->{Color} = shift; } else { # no, it's a getter: $self->{Color}; } }
PHP can do something similar with a structure that matches quite cleanly to the code above. The example below is taken from our upcoming stand-alone form library.
public function name() { if ( func_num_args() ) { $this->_name = func_get_arg( 0 ); return $this; } else { return $this->_name; } }
In the example above, passing a value to the method named name() will set the value and return the object (to allow chaining). If the method is called without a parameter, it return the current value of the object’s name.
This structure is preferable to the magic methods __set and __get for its clarity.
Found this browsing the Sitepoint forums. A great and simple method for building templates in PHP:
class Template extends ArrayObject { protected $file = null; public function setFile( $file ) { $this->file = $file; } public function __toString() { extract($this->getArrayCopy()); ob_start(); include($file); return ob_get_clean(); } } // In your controller... $template = new Template(); $template->setFile( 'myTemplate.phtml' ); // Fictional model method to get rowset from a query $template['rows'] = $db->getRows( $query ); $template['title'] = 'Hello World'; echo $template;
Thanks, Michael Morris.
This first of a series of articles that deal with Less CSS we’ll introduce a fun way to turn an element into a button with the help of a sprite graphic. First comes the sprite map:
Each of the buttons are stacked on top of each other and every one has the same height: 27 pixels.
Next comes the Less mixin. This is where the magic happens:
@webroot: "http://domain.com" .button (@position, @width: 180px) { @buttonHeight: 27px; display: block; height: @height; width: @width; text-indent: -100em; overflow: hidden; border: 0; background: url("@{webroot}/images/buttons-en.png") no-repeat left -(@position * @buttonHeight); }
The above code does a number of things worth going over in detail. @webroot is the root URL of the website. Less compiles all CSS into the current page making it necessary to use absolute URLs instead or URLs relative to the CSS file’s path.
Next, is the mixin declaration. It defines two parameters. The position of the button (starting with 0 for button 1) and the width of the button—set to width of the sprite map.
The button height is then hard-coded and the CSS following uses one of the common image replacement techniques.
Finally comes the background attribute. It says, use the image map positioned to the left of the container. The top of the background is determined by the button number (starting with 0) times the height of each button, subtracted from 0. In other words, button 1 will be at top 0, button 2 at top -27, button 3 at top -54, etc.
The resulting mixin is easy to use.
/** * Makes all buttons of class 'search' use the last image in the sprite set. * Uses the actual width of the 'search' button graphic to prevent trailing white space. */ button.search { .button(3, 82px); } /** * All links of class 'more' use the first button in the set. */ a.more { .button(1, 91px); }
The following error came up on a project built on Django and MySQL:
“Warning: Field ‘id’ doesn’t have a default value”
At first, it seemed as though Django was responsible. However, a quick Google search proved this was an issue with MySQL.
Some solutions returned by the search required the programmer/administrator to export and recreate the database, but the solution proved a lot more simple. For a table named auth_user, the following MySQL command fixes the above error message:
ALTER TABLE auth_user MODIFY `id` INT(11) NOT NULL AUTO_INCREMENT;
The above command resets the ID field of the auth_user table to an auto-incremented integer of length 11, fixing the error message for good.
Google+ may not be Facebook or Twitter but it still has steam. Today, the Plus team released Google+ Pages. Very much like Facebook Pages, Google+ Pages allow your business to broadcast itself and connect with followers via the Circles functionality.
For more on this new feature, check out Google’s own press release below.
http://googleblog.blogspot.com/2011/11/google-pages-connect-with-all-things.html
Pages are not open for everyone yet. Send your email to info@webit.ca and we’ll notify you when they are ready.
Update
After two months of loving labor, the new enRoute Dining Guide app is up on iTunes. Be sure to check it out and download yourself a copy.
Download the enRoute Eats iPhone app.
Content by: Sarah Steinberg (enRoute Online)
Design by: Adrian Kronowetter (Land of Visions)
Development by: Dimitry Zolotaryov (WebIT.ca)
The following terminal command is great for finding those large, hidden files on your hard drive:
cd / && sudo find . -size +1024k -ls
Substitute 1024 with a size you consider big for a file (in kilobytes). In the example, the command sequence will find all files over a megabyte in size.
In the latest iPhone app, it was necessary for a particular view to grow or shrink with respect to its visible subviews. Bellow is a quick snippet from a class called VariableHeightView, a subclass of UIView.
- (CGRect)frame
{
CGRect frame = [super frame];
float height = 0.0f;
for (UIView *asubview in self.subviews) {
if (![asubview isHidden]) {
float subviewBottom = asubview.frame.size.height
+ asubview.frame.origin.y;
if (subviewBottom > height) {
height = subviewBottom;
}
}
}
CGRect newFrame = CGRectMake(frame.origin.x, frame.origin.y,
frame.size.width, height);
return newFrame;
}The code is fairly simple. For a view built using the Interface Builder, the VariableHeightView’s (a superview) frame function will iterative over all subviews, looking for the one lowest in the screen. Once found, it will expand to match that sub-view’s Y origin plus height.
The drawRect function of UIView will take care of rendering the VariableHeightView using the dimension of the frame function.
The array_remove and array_remove_assoc PHP functions allow your code to remove an element from an array (or associative array) given the element’s value. See the comments in the code bellow on how to use the two functions.
<?php /** * Removes the given value from the given array. * * Returns either FALSE if the value was not found in the array * or the index at which the value was found and removed. * * $array = array('a', 'b', 'c', 'd'); * assert( array_remove( 'b', $array ) == 1 ); * assert( array( 'a', 'c', 'd' ) == $array ); * assert( array_remove( 'z', $array ) === false ); * assert( array( 'a', 'c', 'd' ) == $array ); * * @param mixed $val The value to remove * @param array $array The array from which to remove the value * @author Dimitry Zolotaryov, http://webit.ca * @returns FALSE or the index at which the value was found */ function array_remove( $val, &$array ) { foreach ( $array as $i => $v ) { if ( $v == $val ) { array_splice( $array, $i, 1 ); return $i; } } return false; } /** * Removes the given value from the given associative array. * * Returns either FALSE if the value was not found in the array * or the key at which the value was found and removed. * * $array2 = array( 'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4 ); * assert( array_remove_assoc( 1, $array2 ) == 'a' ); * assert( array( 'b' => 2, 'c' => 3, 'd' => 4 ) == $array2 ); * * @param mixed $val The value to remove * @param array $array The associative array from which to remove the value * @author Dimitry Zolotaryov, http://webit.ca * @returns FALSE or the index at which the value was found */ function array_remove_assoc( $val, &$array ) { foreach ( $array as $key => $value ) { if ( $value == $val ) { unset( $array[ $key ] ); return $key; } } return false; }
Updates
-
Can the patent office please stop awarding patents on process and UI?! Swiping to unlock is about as patentable as waving to say "Hello".7 weeks ago from web | Reply, Retweet, Favorite
-
Twitter turns over user information http://t.co/bEySTUkT2 months ago from web | Reply, Retweet, Favorite
-
Trying to remove my data from Twitter, says it can't. https://t.co/xwpvYlQM (the little remove link) So much for functioning privacy.3 months ago from web | Reply, Retweet, Favorite
-
@TheHardBytes Great! Too much choice for Android devices/TVs, little to set them apart ( http://t.co/976A4Kmx ). What's the news in gaming?3 months ago from web | Reply, Retweet, Favorite
-
@TheHardBytes seems like that's the future for all distributable entertainment. Books, movies, games, music. You name it, it's digital.3 months ago from web | Reply, Retweet, Favorite
-
At @rhokMtl with @LieslBarrell . Day 1, code-on!5 months ago from web | Reply, Retweet, Favorite
-
'What does "done" look like?' Don't start a project until you answer this question. @MTLGirlGeeks #TELUShosting
-
Random Hacks of Kindness in Montreal. Join an enthusiastic team of professionals and build something great: http://t.co/vW50GX0b, @mtlpy6 months ago from web | Reply, Retweet, Favorite
-
My very first interview on Web Central Station. http://t.co/sd3bp5k11 months ago from web | Reply, Retweet, Favorite
-
Ever heard of a study wherein those who unknowingly paid more for a item than others experience a diff emotional reaction towards said item?11 months ago from web | Reply, Retweet, Favorite
-
11 months ago from web | Reply, Retweet, Favorite
-
O-pacity! You animate worse than every other property.12 months ago from web | Reply, Retweet, Favorite
-
Serif fonts are great. But please up the font size to 15+ http://bit.ly/l1QyDY12 months ago from web | Reply, Retweet, Favorite
-
Concrete tent! http://www.bbc.co.uk/news/technology-1343074712 months ago from web | Reply, Retweet, Favorite
-
Monopoly introduced digital currency; indebted players no longer "in-touch" with their means default on their Park avenue property.12 months ago from web | Reply, Retweet, Favorite
-
TV.com shows a "connect with Facebook" overlay when loading any inside page (http://www.tv.com/watch_episodes).12 months ago from web | Reply, Retweet, Favorite
-
Hotmail is broken. That will teach me to use hotmail.12 months ago from web | Reply, Retweet, Favorite
-
Flavors.me becomes a musician's refuge from MySpace. Includes artists like OK Go http://flavors.me/okgo13 months ago from web | Reply, Retweet, Favorite
-
Best sampling of Chic's My Fobidden Lover: Luther Vandross's Shine http://bit.ly/3oa8mv or Blk I Peas' Fashion Beats http://bit.ly/eCpmkm?14 months ago from web | Reply, Retweet, Favorite
Python and PHP developer living in Montreal. My site.