Tim Barsness http://www.timbarsness.com/feed en-us http://blogs.law.harvard.edu/tech/rss Sweetcron tbarsness+timbarsness.com@gmail.com How do I use traits in PHP 5.4 http://www.timbarsness.com/items/view/840 The biggest change in PHP 5.4 is the addition of traits to the object-oriented programmability. Traits prevent code duplication by allowing multiple classes to include a collection of methods implemented as a trait. This means multiple classes can include the same method implementations without duplicating code. Traits improve on interfaces because they include not only the function definition, but also the implementation. Traits can also use other traits, meaning that one trait could be a collection of others. So, if you had traits Roll and Bounce, you could have trait Movements that used both Roll and Bounce.

trait HelloWorld { public function sayHello() { echo 'Hello '; }   public function sayWorld() { echo 'World!'; } }   class MyClass { use HelloWorld; public function sayHelloWorld() { $this->sayhello(); $this->sayWorld(); } }   $h = new MyClass(); $h->sayHelloWorld(); // outputs "Hello World!"

In implementing traits, the keywords trait and insteadof were added to PHP. Usage of trait is outlined above. The PHP keyword insteadof is used for conflict resolution. Here’s an example of how to use insteadof:

trait A { public function smallTalk() { echo 'a'; } public function bigTalk() { echo 'A'; } }   trait B { public function smallTalk() { echo 'b'; } public function bigTalk() { echo 'B'; } }   class Talker { use A, B { B::smallTalk insteadof A; A::bigTalk insteadof B; } }   class Aliased_Talker { use A, B { B::smallTalk insteadof A; A::bigTalk insteadof B; B::bigTalk as talk; } }

]]>
Tue, 31 Jan 2012 14:12:00 +0000 http://serversideguy.com/2012/01/31/how-do-i-use-traits-in-php-5-4/
timbarsness: @ChipotleMedia -- lucky! I've only been to @shophousetweets once. Need an excuse to go back to DC. http://www.timbarsness.com/items/view/839 Tue, 24 Jan 2012 16:36:00 +0000 http://twitter.com/timbarsness/statuses/161849922470162432 timbarsness: @LizLabuz <a href="http://t.co/Cd8JCbOf" rel="external">http://t.co/Cd8JCbOf</a> http://www.timbarsness.com/items/view/838 Fri, 20 Jan 2012 13:14:00 +0000 http://twitter.com/timbarsness/statuses/160349490988056576 timbarsness: @mitchellhislop you at #minnedemo? http://www.timbarsness.com/items/view/837 Thu, 19 Jan 2012 01:35:00 +0000 http://twitter.com/timbarsness/statuses/159811166422573056 timbarsness: @TyOlson @smcpros: Name your players... http://www.timbarsness.com/items/view/836 Sat, 31 Dec 2011 04:08:00 +0000 http://twitter.com/timbarsness/statuses/152964425333227522 timbarsness: Hey, @TyOlson, I challenge you to a fuel: <a href="http://t.co/YCWrPYbG" rel="external">http://t.co/YCWrPYbG</a> http://www.timbarsness.com/items/view/835 Sat, 31 Dec 2011 01:28:00 +0000 http://twitter.com/timbarsness/statuses/152923932759044098 timbarsness: "So, what do you think of qdoba? Because I like it better..." @TyOlson http://www.timbarsness.com/items/view/834 Fri, 30 Dec 2011 01:25:00 +0000 http://twitter.com/timbarsness/statuses/152560816036646913 timbarsness: @ReedDaniels Delicious indeed. Love @ChipotleTweets http://www.timbarsness.com/items/view/832 Thu, 29 Dec 2011 21:09:00 +0000 http://twitter.com/timbarsness/statuses/152496409281708032 timbarsness: @mitchellhislop Oooh very nice!! Time well spent. http://www.timbarsness.com/items/view/833 Thu, 29 Dec 2011 20:20:00 +0000 http://twitter.com/timbarsness/statuses/152484258810839040 Project Euler Problem 17 http://www.timbarsness.com/items/view/831 I’ve been having some fun doing the first few problems of Project Euler and figured I’d share my solution to problem 17 here.
The Problem If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total. If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used? NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. The use of “and” when writing out numbers is in compliance with British usage. The Solution The reason I love this solution is I found a PHP library that writes out these numbers for you. It comes from PECL 1.0 and is part of PHP 5.3 via that NumberFormatter class. This class did most of the work for me.

<? $res = "";   $f = new NumberFormatter("en", NumberFormatter::SPELLOUT);   for($i = 1; $i format($i)); if($i > 100 && $i % 100 != 0) { $res .= 'and'; } } echo strlen($res)."\n";

My first submission was incorrect and I quickly figured out it was because the NumberFormatter class was missing the “and” after the hundreds. I simply added that at the end since we’re only looking for a count and came up with the correct result. Installing the NumberFormatter class in Debian squeeze was pretty easy. I simply had to install the PHP5 international compatibility package: sudo apt-get instal php5-intl

]]>
Wed, 28 Dec 2011 04:17:00 +0000 http://serversideguy.com/2011/12/27/project-euler-problem-17/
How do I perform integer division in PHP http://www.timbarsness.com/items/view/830 PHP’s division operator returns a floating point result from the division operator unless both operands are integers and they are evenly divisible (the result is an integer). I was recently working on Project Euler Problem 13 and needed to do integer division, something I haven’t come across in PHP before.
Integer division in PHP is pretty straight forward. Casting the result as an integer will yield an integer. That is:

var_dump(25/7); // float(3.5714285714286) var_dump((int) (25/7)); // int(3) var_dump(round(25/7)); // float(4)

]]>
Wed, 28 Dec 2011 02:34:00 +0000 http://serversideguy.com/2011/12/27/how-do-i-perform-integer-division-in-php/
Project Euler Problem 10 http://www.timbarsness.com/items/view/829 I’ve been having some fun doing the first few problems of Project Euler and figured I’d share my solution to problem 10 here.
The Problem The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million. The Solution My solution is fairly simple, testing primality for all the numbers below 2,000,000. If prime, adding them to a running total.

/** * Calculate the sum of all the primes below two million. **/

include

include

using namespace std;   bool isPrime(long num) { if(num<=1) return false; for(long i=2; i<=sqrt(num*1.0); i++) { if(num%i==0) return false; } return true; }   int main () { long upperlimit = 2000000, sum = 0;   for (long i = 2; i < upperlimit; i++) { if(isPrime(i)) { sum += i; cout << "Prime found: " << i << endl; } }   cout << "The sum of primes below " << upperlimit << " is " << sum << endl;   return (0); }

The interesting part of this problem for me was I updated my primality test to include the fact that if a number n has a factor greater than sqrt(n) it must also have one less than sqrt(n), so we only have to test up to sqrt(n) in our primality test:

bool isPrime(long num) { if(num<=1) return false; for(long i=2; i<=sqrt(num*1.0); i++) { if(num%i==0) return false; } return true; }

]]>
Fri, 23 Dec 2011 18:12:00 +0000 http://serversideguy.com/2011/12/23/project-euler-problem-10/
Project Euler Problem 9 http://www.timbarsness.com/items/view/828 I’ve been having some fun doing the first few problems of Project Euler and figured I’d share my solution to problem 9 here.
The Problem A Pythagorean triplet is a set of three natural numbers, a b c, for which,a^2 + b^2 = c^2 There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc. The Solution This is pretty basic and inefficient, but I decided to loop through a and b, calculating c from the difference.

include

using namespace std;   int main () {   for(int a = 1; a < 1000; a++) { for(int b = 1; b < 1000; b++) { cout << "a: " << a << " b: " << b << " c: " << (1000 - a - b) << " a^2 + b^2: " << (a * a + b * b) << " c^2: " << (1000 - a - b) * (1000 - a - b) << endl; if(a * a + b * b == (1000 - a - b) * (1000 - a - b)) { return (0); } } } return (0); }

There you have it, the program stops at a^2 + b^2 = c^2 where c = (1000 – a – b). I then took abc to get the answer.

]]>
Thu, 22 Dec 2011 21:37:00 +0000 http://serversideguy.com/2011/12/22/project-euler-problem-9/
timbarsness: Just saw a bear #spyparty http://www.timbarsness.com/items/view/827 Sat, 17 Dec 2011 02:08:00 +0000 http://twitter.com/timbarsness/statuses/147860801687007232 Project Euler Problem 7 http://www.timbarsness.com/items/view/826 I mentioned yesterday, I’ve been doing the first few problems of Project Euler and figured I’d share my solution to problem 7 here. The Problem By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the 10,001st prime number? The Solution Let’s define a function isPrime(num) (granted, could be more efficient) and loop through the natural numbers, counting those that are prime.

/** * Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum. **/

include

using namespace std;   bool isPrime ( unsigned long num ) { for(int i = num - 1; i > 1; i--) { if(num % i == 0) { return false; } } return true; }   int main () { unsigned long num = 2, primeCount = 0;   while ( primeCount < 10001 ) { if ( isPrime (num) ) { primeCount++; cout << "Prime " << primeCount << " " << num << " is prime" << endl; } num++; }   return(0); }

]]>
Thu, 15 Dec 2011 18:32:00 +0000 http://serversideguy.com/2011/12/15/project-euler-problem-7/
Project Euler Problem 7 http://www.timbarsness.com/items/view/825 I mentioned yesterday, I’ve been doing the first few problems of Project Euler and figured I’d share my solution to problem 7 here. The Problem By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the 10,001st prime number? The Solution Let’s define a function isPrime(num) (granted, could be more efficient) and loop through the natural numbers, counting those that are prime.

/** * Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum. **/

include

using namespace std;   bool isPrime ( unsigned long num ) { for(int i = num - 1; i > 1; i--) { if(num % i == 0) { return false; } } return true; }   int main () { unsigned long num = 2, primeCount = 0;   while ( primeCount < 10001 ) { if ( isPrime (num) ) { primeCount++; cout << "Prime " << primeCount << " " << num << " is prime" << endl; } num++; }   return(0); }

]]>
Thu, 15 Dec 2011 13:57:00 +0000 http://serversideguy.com/2011/12/15/project-euler-problem-7/
Project Euler Problem 6 http://www.timbarsness.com/items/view/824 I mentioned yesterday, I’ve been doing the first few problems of Project Euler and figured I’d share my solution to problem 6 here. The Problem Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum. The Solution This one is pretty straight forward. Find a data structure capable of holding the square of the sum of numbers 1 to 100 and subtract the squares of the first 100 natural numbers.

include

using namespace std;   int main () { unsigned long sumsquares = 0, sums = 0; int n;   cout <> n;   while (n < 0) { cout <> n; }   for(int i = 1; i <= n; i++) { cout << "i: " << i << endl; sumsquares += i * i; cout << "sumsquares: " << sumsquares << endl; sums += i; cout << "sums: " << sums << endl; }   cout << "The sum of squares from 1 to " << n << ": " << sumsquares << endl; cout << "The square of sums from 1 to " << n << ": " << sums * sums << endl << endl; cout << "The difference: " << (sums * sums - sumsquares) << endl;   return(0); }

As you can see, there is a loop creating the sum and the sum of squares. From there, it subtracts the square of the sum and the sum of squares to get the answer.

]]>
Thu, 15 Dec 2011 02:26:00 +0000 http://serversideguy.com/2011/12/14/project-euler-problem-6/
Project Euler Problem 5 http://www.timbarsness.com/items/view/823 I’ve been having some fun doing the first few problems of Project Euler and figured I’d share my solution to problem 5 here.
The Problem 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? The Solution This problem, worded another way, asks what the least common multiple of all the integers between 1 and 20 is. Since multiplication is commutative, the least common multiple of a series is the same as computing the least common multiple of all the numbers before it and the current. So, LCM(1…6) is the same as LCM(6, LCM(5, LCM(4, LCM(3, LCM(2, 1))))).

include

using namespace std;   unsigned long gcd(unsigned long a, unsigned long b) { if(b == 0) return a; return gcd(b, a%b); }   unsigned long lcm(unsigned long a, unsigned long b) { return a * b / gcd(a,b); }   int main () { unsigned long answer = 1; for(long i = 1; i <= 20; i++) { cout << "i: " << i; answer = lcm(answer, i); cout << " lcm: " << answer << endl; }   return 0; }

My solution uses an algorithm to compute the least common multiple that involves finding the greatest common divisor as follows: lcm(a, b) = a*b / gcd(a, b). I used the Euclid method of finding the greatest common divisor: gcd(a,0) = a; gcd(a, b) = gcd(b, a mod b).

]]>
Wed, 14 Dec 2011 00:50:00 +0000 http://serversideguy.com/2011/12/13/project-euler-problem-5/
How do I iterate through recently modified posts in WordPress? http://www.timbarsness.com/items/view/822 I have a client who has a WordPress installation with thousands of pages. There are a couple things wrong with this, mainly relating to speed and editing practicality. It’s also an issue to find pages because they are ordered hierarchically. There are a few pages I modify fairly regularly and wanted a convenient way to find them. In order to do so, I needed to iterate through recently modified pages. Here’s what I came up with:

function recently_modified_dashboard_widget_function() { global $wpdb;   $querystr = " SELECT $wpdb->posts.* FROM $wpdb->posts WHERE $wpdb->posts.post_type = 'page' ORDER BY $wpdb->posts.post_modified DESC LIMIT 0,25 ";   echo "<ul>\n"; foreach($wpdb->get_results($querystr, OBJECT) as $post) { echo '<li>'.edit_post_link( str_replace( site_url(), '', get_permalink( $post->ID ) ), '', '', $post->ID )."</a></li>\n"; } echo "</ul>\n"; }

The query gets the last 25 posts of type page ordered by post_modified. From there, a foreach loop through the result yields individual posts in object form. The I used the post’s permalink, stripping off the site_url() as the text for the edit_post_link() text. All is well. I’ll soon publish the plugin which adds the list to the admin dashboard.

]]>
Thu, 08 Dec 2011 23:38:00 +0000 http://serversideguy.com/2011/12/08/how-do-i-iterate-through-recently-modified-posts-in-wordpress/
What is the E-Myth http://www.timbarsness.com/items/view/821 The E-Myth is a myth in the country that people start businesses because they are entrepreneurs.  That is, they start businesses to take on risk in order to make capital.  According to Michael Gerber, the real reason people start businesses is to get rid of their boss.  Most people who start businesses are currently in a technical role.  “Any idiot can run a business,” they think, it’s the technical work that needs to be done.  So for those type of people, they start a business in order to not have a boss. People who start a business have three distinct personalities:  the entrepreneur, the manager and the technician. The Entrepreneur is the dreamer, always thinking ahead, pushing the limits and leaving destruction in his path. The Manager is the organizer, trailing behind the entrepreneur, struggling to keep up, organizing things in the entrepreneur’s wake. The Technician is the doer, loves getting things done, if you want it done right, you have to do it yourself.  The technician thinks of the entrepreneur as simply creating more work for him; and, thinks of the manager as the person holding him back from doing actual work. If you put the three together, you end up with an incredibly capable individual.  However, most people who start businesses are only 10% entrepreneur, 20% manager and 70% technician.  This leads to the wrong person being at the helm – the technician is in charge.

]]>
Fri, 02 Dec 2011 17:03:00 +0000 http://serversideguy.com/2011/12/02/what-is-the-e-myth/