O GoConqr está de visual novo! Conte para nós o que você achou.

Flashcards

PHP constructs

Ben Parish PHP Inglês 17 flashcards

Flashcards do conjunto (17)

  1. 1What is the difference between using self and $thisUse $this to refer to the current object. Use self to refer to the current class. In other words, use $this->member for non-static members, use self::$member for static members.
  2. 2What are traits useful for?If you want two completely independent classes to be able to use the same method you would have previously had to have created a superclass that they extend from.
    With traits you can allow completely independent classes to share the same methods.
  3. 3What are late static bindings for?Allows the abstract or superclass to reference the calling class that extends it. If self is used, it will only refer to the superclass and not the calling class.
    It also calls the method in the calling class by default and if none is found it will use its own method.
  4. 4Why might you want to extend the Exception class?First, you can extend the class’s functionality.
    Second, the fact that a derived class defines a new class type can aid error handling in itself.
  5. 5Why would you use 'finally' in a try/catch cause?A finally clause will be *ALWAYS run if an invoked catch clause rethrows an exception or returns a value.
    } catch ( FileException $e ) {
    fputs( $fh, "file exception\n" );
    throw $e;
    } finally {
    fputs( $fh, "end\n" );
    fclose( $fh );
    }
  6. 6What is Delegation?Delegation (composition) is the mechanism by which one object passes method invocations on to a second.
    class PersonWriter {
    function writeName( Person $p ) {
    print $p->getName()."\n";
    }
    function writeAge( Person $p ) {
    print $p->getAge()."\n";
    }
    }
  7. 7Why are callbacks useful?They allow you to plug functionality into a component at runtime that is not directly related to that component’s core task. By making a component callback aware, you give others the power to extend your code in contexts you don’t yet know about.
  8. 8How do you get a fully qualified string reference to a class?PHP 5.5 introduced the ClassName::class syntax. In other words, given a class reference, you can append a scope resolution operator and the class keyword to get the fully qualified class name. So this:
    print u\Writer::class."\n";
    Ouputs this:
    util\Writer
  9. 9What are anonymous functions?An anonymous function is simply a function with no name.
    // Anonymous function
    function () {
    return "Hello world";
    }
  10. 10What are lambdas?A Lambda is an anonymous function that can be assigned to a variable or passed to another function as an argument.
    Lambda functions allow the quick definition of throw-away functions that are not used elsewhere.
    $greeting = function () {
    return "Hello world";
    }
    // Call function
    echo $greeting();
    // Returns "Hello world"
    or
    You could also pass the function to another function, like this:
    // Pass Lambda to function
    function shout ($message){
    echo $message();
    }
    // Call function
    shout(function(){
    return "Hello world";
    });
  11. 11Why would you want to use a Lambda?Lambdas are useful because they are throw away functions that you can use once. Often, you will need a function to do a job, but it doesn’t make sense to have it within the global scope or to even make it available as part of your code. Instead of having a function used once and then left lying around, you can use a Lambda instead.
    Of course, you have been able to use the create_function function in PHP for a while now. This basically does the same job.
    // Use create_function
    $greeting = create_function('', 'echo "Hello World!";');
    // Call function
    $greeting();
  12. 12What are closures?A Closure is essentially the same as a Lambda apart from it can access variables outside the scope that it was created.
    It enables the anonymous function to track variables from its wider scope with a use clause:
    return function( $product ) use ( $amt, &$count ) {
    }
    NOTE:By default, all imported variables are copied as values into the closure. This makes it impossible for a closure to modify the variable in the parent scope. By prepending an & in front of the variable name in the use declaration, the variable is imported as a reference instead. In that case, changes to the variable inside the closure will affect the outside scope.
  13. 13When would you use Closures?Closures are useful when using PHP functions that accept a callback function like array_map, array_filter, array_reduce or array_walk.
    The array_walk function takes an array and runs it through the callback function.
    // An array of names
    $users = array("John", "Jane", "Sally", "Philip");
    // Pass the array to array_walk
    array_walk($users, function ($name) {
    echo "Hello $name
    ";
    });
    // Returns
    // -> Hello John
    // -> Hello Jane
    // -> ..
  14. 14Give 7 features of closures1. The closure is a function assigned to a variable, so you can pass it around
    2. A closure is a separate namespace, normally, you can not access variables defined outside of this namespace. There comes the use keyword:
    3. use allows you to access (use) the succeeding variables inside the closure.
    4. use is early binding. That means the variable values are COPIED upon DEFINING the closure. So modifying $tax inside the closure has no external effect, unless it is a pointer, like an object is.
    5. You can pass in variables as pointers like in case of &$total. This way, modifying the value of $total DOES HAVE an external effect, the original variable's value changes.
    6. Variables defined inside the closure are not accessible from outside the closure either.
    7. Closures and functions have the same speed. Yes, you can use them all over your scripts.
  15. 15What does SPL stand for and what is it used for?The Standard PHP Library (SPL) was added in PHP 5.0. It provides a number of very useful facilities that expose some of PHP’s internal functionality and allow the “userland” developer to write objects that are capable of behaving like arrays, or that transparently implement certain iterative design patterns to PHP’s own core functionality
  16. 16Give examples of iterations that SPL allows- Array Access to objects
    - Simple Iteration
    - Seekable Iteration
    - Recursive Iteration
    - Filtered Iteration
  17. 17What are generators?Introduced in PHP 5.5, generators were intended to allow you to create simpler iterators, without needing to write a bunch of boilerplate code.

Informações

Descrição
PHP (Flashcards) FlashCards sobre PHP constructs, criado por Ben Parish em 02-11-2015.
Criado por
Ben Parish
Visualizações
56
Avaliação

Fale Conosco

Sobre o que é sua mensagem?

O que você acha do novo visual?

Você gostou?