June 30, 2009

zend certification preparation ::: Arrays and functions

It's been a long time since I am posting something about ZCE. Just because of enormous pressure of thesis work and office works .. i couldn't managed time for taking preparation.

However I am starting again now. I will now try to concentrate about php arrays and functions and try to find answers about some tricky questions.

  • In php a class is declared using the class construct

  • <?php
    class my_class
    {
    var $myVar;
    function my_class ($var)
    {
    $this->my_var = $var;
    }
    }
    ?>


  • Object is instantiated by
    $obj = new my_class (“something”);

  • So for previous class example
    $obj->my_var; 
    can fetch the value

  • For using any of the methods of the class, we can use "namespace" concept. Let us consider we have a class called 'myclass' and it has a method named calc(). So now to access the method we can use the following code:

    <?php
    class myclass
    {
    var $var1;
    function myclass ($value)
    {
    $this->var1 = $value;
    }

    function calc($val)
    {
    $val = pow($val,2);
    return $val;
    }
    }

    echo myclass::calc(16);
    ?>

    This code will yield 256.

  • A class is a blueprint of an object, which is an instance of a class.

  • Object-oriented pattern used to implement a class that must be instantiated only once for the entire lifespan of a script is ---- Singleton Pattern

  • Constructor that can be used for a class ---- in php5 both classname() and __classname() can be used. But in php4 only classname() is supported


  • In php4 the following code will output 5 as there objects are treated as scalar ways and copying object like "$b=$a" makes only another copy so the later assignment does not make sense.

    But in php5, the output will be 10.

    <?php
    class my_class
    {
    var $value;
    }
    $a = new my_class;
    $a->my_value = 5;
    $b = $a;
    $b->my_value = 10;
    echo $a->my_value;
    ?>


  • In php parent constructors are not automatically called from a child class’ constructor—they have to be executed explicitly. This is true of any other class member (variables etc.).

  • To sort an array in ascending order by value while preserving key
    associations the PHP sorting function in action will be --- "asort"

  • The name of a function used to convert an array into a string is --- "serialize" .... "implode" can also be used for this purpose

  • "natsort()" function uses a “natural ordering” algorithm to sort the contents of an array,rather than a simple binary comparison between the contents of each element

  • "array_flip()" swaps each element of the array with its index

  • "rsort()" and "array_reverse()" reorder an array in reverse order that is if we consider
    $a = array('x','y','z');
    as a reference,
    rsort($a);
    and
    array_reverse($a);
    gives us the same output which is
    array('z','y','x')

  • The "array_sum()" function calculates the sum of all the elements of an array.

  • Only integer numbers and strings can be used as keys of an array; floating point numbers are converted to integers like 1.2 and 1.5 as keys of and array will result the array as having only 1 element with key 1 and it will take the value of last index that is the the value of index 1.5


To be continued .... [until tomorrow :P]

2 comments:

  1. You may have a trouble with addiction and need info on California treatment centers this is a resource that you can trust.

    ReplyDelete
  2. Thanks for the helpful info. When I learned about energy balancing it changed my life.

    ReplyDelete