«

»

Apr 22

Print this Post

Talks by AKAI & Wikia

Last weekend we started all Engineering All Hands 2012. It made my days even shorten than before. Finally, I’m relaxing this weekend: sleeping, playing games, taking long walks, reading… and (I made it!) writing. This time it will be just a short post about cooperation between AKAI, which is web-application fans’ club gathering students from Poznań’s universities, and Wikia.

Our work on talks for students started in November 2011. We organized three meetings so far and at the last meeting I gave it a try and also talked a little bit about PHP basics. I must say I was really nervous but I got impression that my presentation wasn’t as bored as I thought it had been ;) I even had few questions to answer and about one of them I’d like to write more.

The question was about “class variables” which exist in Python — do they exist it PHP as well? I wasn’t sure what was the answer because I didn’t know Python at all. But after my honest answer about lack of Python skills the questioner told me that those are variables which values doesn’t change and is the same between different instances of a class. Of course, the answer was pretty simple and the rest of the audience answered the question. You can use in PHP something which is static field of a class:

class A {
        public $b = 0;
        public static $c = 0;

        public function incrB() {
                $this->b++;
        }

        public function incrC() {
                self::$c++;
        }

        public function display() {
                print_r(array(
                        $this->b,
                        self::$c
                ));
        }
}

$a1 = new A;
$a2 = new A;
$a3 = new A;

$a1->display();
$a2->display();
$a3->display();

$a2->incrB();
$a2->incrC();

$a1->display();
$a2->display();
$a3->display();

$a2->incrB();
$a2->incrC();
$a3->incrB();
$a3->incrC();

$a1->display();
$a2->display();
$a3->display();

After running this simple script you can notice how the static field $c “remembers” its previous value. This value is also shared between different instances of the class. So, at the beginning the value equals 0. We increase it by firing incrC() method of object $a2. Later we display values and for each instance ($a1, $a2, $a3) the value of $c equals 1. Then we increase it twice by firing incrC() method of object $a2 and the same named method of object $a3. The value of $c static field for each instances equals 3 at the end of the script.

I went through the English and Polish documentation chapters reading about classes and I didn’t find anything highlighted about this behavior of “class variables” in Python. There was also nothing about static fields. Therefore I assumed in Python all variables of a class behaves like that. And that’s why author of the question called it “class variables”. But maybe I’m wrong. If so, I’ll change this part of the post soon ;)

I would also invite you to next event by AKAI and Wikia which will take place at Poznań University of Technology on May 8th. More information (unfortunately only in Polish) here. Here you can find slides from my previous talk + all slides from “Front-end i… w praktyce” events are available here.

Permanent link to this article: http://blog.lukaszewski.it/2012/04/22/talks-by-akai-and-wikia/

1 comment

  1. Luke

    Hi Andrew!
    thx 4 interest of this case ..:) (House said….)


    #!/usr/bin/python

    class Employee:

    empCount = 0

    def __init__(self, name, salary):
    self.name = name
    self.salary = salary
    Employee.empCount += 1

    def displayCount(self):
    print "Total Employee %d" % Employee.empCount

    def displayEmployee(self):
    print "Name : ", self.name, ", Salary: ", self.salary

    "This would create first object of Employee class"
    emp1 = Employee("Zara", 2000)
    "This would create second object of Employee class"
    emp2 = Employee("Manni", 5000)
    emp1.displayEmployee()
    emp2.displayEmployee()
    print "Total Employee %d" % Employee.empCount

    RESULTS:
    Name : Zara ,Salary: 2000
    Name : Manni ,Salary: 5000
    Total Employee 2

    so in this code class variable is empCount, and U calling her by class name, this is alternative way to static. Anyway is good to know that, sometimes is needed some stuff like this in PHP . But case looks more like this in elier is:


    class A {
    public $b = 0;
    public static $c = 0;

    public function __construct() {
    self::$c++;
    $this->b++;
    }

    public function display($obj_name) {
    var_dump($obj_name, array(
    $this->b,
    self::$c
    ));

    }
    }

    $a1 = new A;
    $a2 = new A;
    $a3 = new A;

    $a1->display('a1');

    is nice way, cos We creating 3 different objects but counter is one for all …

    anyway… needed or not…good 2 know..
    see ya. Luke

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>