PHP 5.4 New Features - Traits

December 05, 2012 at 19:22 | 0 comments

Another great thing that has been added to PHP are the traits. They are an old conception in other programming languages such as Java or C++. They are extremely useful in order to write reuseable code and make your life easier. So what we can do, just to make a quick introduction, is to write down a class which is using the properties and methods of two traits.

trait Weapons {
    private $_weapons = array(1 => 'a sword', 2 => 'an axe', 3 => 'a spear');

    public function choose_weapon($id) {
        echo ' using ' , $this->_weapons[$id];
    }
}

trait Professions {
    private $_professions = array(1 => 'Warrior', 2 => 'Hunter');
}

class Hero {
    use Weapons, Professions; //here we say which traits our class will have access to

    public function __construct($profession) {
        echo $this->_professions[$profession];
    }
}

$hero = new Hero(1);
$hero->choose_weapon(2);

Result:

Warrior using an axe

So the deal is that we don't have to extend the Hero class in order to access the traits. And the best thing is that we can reuse the traits as many times as needed. A problem will accure if we create two methods with the same name, but I am pretty sure that it will be fixed very soon!

more

PHP 5.4 New Features - Built-in Web Server

November 28, 2012 at 9:38 | 0 comments

One of the new things that I like most in PHP is definitely the built-in webserver feature. But do not think of it as a replacement of the very popular Apache or Nginx web servers. It has been created for development purpose only and you should never use it in production.

You can start your PHP web server via the command line of your computer. There shouldn't be any problems as long as you have PHP version 5.4 above. Simply type:

php -S localhost:80

Note that you must specify which port to be used.

You can start a specific file or directory on your server by adding the path at the end, after the port number.

php -S localhost:80 /subdirectory/another_subdirectory/my_file.php

 

There is a big chance that you will get an error if you are on Windows:

'php' is not recognized as an internal or external command, operable program or batch file.

This is because you have tell Windows manually how to deal with "php". You can fix this by adding an environment variable to your system.

1. Open System Properties by typing in the start menu "View advanced system settings" or navigate to there by right-clicking the My Computer menu -> Properties -> Advanced System Settings -> Environment variables;

2. Find the system variable Path and click Edit.

3. Add your the path to your php directory at the end of the Variable value, separating with a semicolon.

more

Why Using MySQL Functions Is Bad?

November 22, 2012 at 20:41 | 0 comments

First of all, let us to get clear which exactly are the MySQL functions. link

So why I say that they are bad to use? Well I personally do not. PHP is changing, so is every other language. After about 10 years of successful usage, these functions are officially deprecated and some day they in the bright future they will be removed.

What should I use then? There are many other ways to manage your database. Php.net suggests us to use the MySQL Improved Extension and PDO (PHP Data Object). Not to mention what kind of advanced ORMs (Object-Relational Mapping) exist.

Try not to use these functions anymore. With some little bit of digging into, for example, PDO you will understand the difference and you will not regret for doing so.

more

List of Irregular Verbs in English

May 20, 2012 at 20:44 | 0 comments

A list of most verbs in English. It's the perfect weapon for exams (+ mobile support)!

http://irregular-verbs.ava7.eu/

more

EXIF and PHP, how to find where have you been

April 16, 2012 at 15:51 | 0 comments

This post got my attention (link). I won't talk about conspiracy theories and stuff. I knew that images store some kind of personal information but I didn't know how to extract it and may be I never asked myself how to do it. So after I saw this post by Gatakka, I decided to dig into the deep and do something creative:

geoimg.ava7.eu

It's very simple actually and it took me several hours to build it. All you need to know is how to work with exif_read_data function. Here's the class that makes the big deal. I hope you'd like it:

class Exif {

    private $_lat = 0, $_long = 0;
    private $_basicInfo = array();

    public function checkExif($img) {
        $arr = exif_read_data($img, 'GPS');
        $this->_basicInfo = array(
            'Model' => $arr['Model'], 'Produced by' => $arr['Make'], 'Date taken' => $arr['DateTime'],
            'Sixes' => $arr['COMPUTED']['html']
        );
        if ($arr) {
            $this->_lat = explode('/', $arr['GPSLatitude'][1]);
            $this->_lat = $this->_lat[0] / $this->_lat[1];
            $this->_long = explode('/', $arr['GPSLongitude'][1]);
            $this->_long = $this->_long[0] / $this->_long[1];

            //get degrees
            $dlat = explode('/', $arr['GPSLatitude'][0]);
            $dlong = explode('/', $arr['GPSLongitude'][0]);
            //get seconds
            $slat = explode('/', $arr['GPSLatitude'][2]);
            $slong = explode('/', $arr['GPSLongitude'][2]);

            $this->_lat = $this->_calc($dlat[0], $this->_lat, $slat[0]);
            $this->_long = $this->_calc($dlong[0], $this->_long, $slong[0]);

            if (strtoupper($arr['GPSLatitudeRef']) == 'S') {
                $this->_lat = '-' . $this->_lat;
            } if (strtoupper($arr['GPSLongitudeRef']) == 'W') {
                $this->_long = '-' . $this->_long;
            }
            return true;
        } else {
            return false;
        }
    }

    private function _calc($degree, $min, $sec) {
        //Decimal degrees = whole number of degrees, plus minutes divided by 60, plus seconds divided by 3600
        return number_format($degree + ($min / 60) + ($sec / 3600), 5);
    }

}

It doesn't work with every single picture. First, file format should be either tif or jpg and second - the image must contain GPS information. Have fun using it! :)

more

date function, how can I display a non-english name of a month

March 15, 2012 at 20:33 | 0 comments

The function date gives us pretty nice features when working with time and dates. Working with this function makes things so easy. But there is one little problem for those who are going to use the function with a language other than English. It took me some time before I could figure out how could I possibly display month names in Bulgarian language. It is actually very easy and it works every language.

The problem is that when you open the documentation, you see a huge list of all available date formats. And then you keep look for the thing that exactly fits your taste (or at least I used to do that). But it looks like that not everything is written in the Internet and sometimes you just need to work it out on you own:

echo date('J', time()); //outputs the current month, by this time March

Hey wait, I want cyrilic? No problem, here you go:

$array = [1 => 'Януари', 2 => 'Февруари', 3 => 'Март', 4 => 'Април', 5 => 'Май', 6 => 'Юни', 7 => 'Юли', 8 =>  'Август', 9 => 'Септември', 10 => 'Октомври', 11 => 'Ноември', 12 => 'Декември'];

//php 5.4 supports shorter way to create arrays, fuck yeah!

And now instead of showing the exact month, we should use the n format which will output the number of the month (1 to 12):

echo 'The current month is ' . $array[ date('n', time()) ];

Using the same way you can create an array for the days of the week and have more fun.

more

iWebKit or how to create web applications for iPhone

January 21, 2012 at 17:54 | 0 comments

I have always wanted to create applications for telephones. But I never found the time to actually take a look on these SDKs for Android and iPhone. Anyways, a few days ago a friend of mine showed me this pretty little thing called iWebKit. It's basically some javascript and css which make your website looks like an iPhone app. Now I have never had an iPhone or any iOS device, but as far as I know, you can add URL links to websites straight on your iPhone's desktop. Which would allow quick access to your app (website). Here's a little demo of one minute work with the iWebKit:

http://ios.ava7.eu/

You can create pretty cool stuff with just some basic knowledge in any web programming languages or even simple HTML. As I love to say, the only limit is your own imagination.

One more thing. As some of you may know what webkit is, the answer is yes you need a webkit browser (Google Chrome or Safari). But don't worry as an iPhone user you will have Safari for sure. The application can works not only on iPhone, don't forget that it's a website!

And finally, I should not forget to give some credits to the person who let me know how to create iPhone applications without having a Mac - my dear friend Michael.

more

friends

find me

  • google plus
  • linked in
  • youtube