Friday 1 February 2019

PHP 7 New Features

PHP 7 New Features

Question: What are main features of PHP 7?
  1. Lower Memory Consumption
  2. Improved performance as now using Zend Engine 3.0
  3. Consistent 64-bit support
  4. Scalar type declarations
  5. Return type declarations
  6. define() updates: Now you can add array.
  7. unserialize updates: Provide better security when unserializing objects on untrusted data
  8. Anonymous classes can be created
  9. Null coalescing operator (??) have been added
    Namespaces declaration like use some\namespace\{ClassA, ClassB, ClassC as C};
  10. Many fatal errors converted to Exceptions
  11. Secure random number generator
  12. session_start will accept an array.
  13. Spaceship operator
  14. list() function updates



Question: What is null coalescing operator?
null coalescing operator (??) has been introduced in PHP7.
$username = $_GET['username'] ?? $_POST['username'] ?? 'not passed';
print($username);



Question: What are Scalar type declarations?
Scalar type declarations has two type of options.
  1. coercive - coercive is default mode and need not to be specified.
  2. strict - strict mode has to explicitly hinted.



Question: What is Namespaces?
Namespaces are a way of encapsulating items. we can use two more different classes into one Namespaces.


Question: What is purpose of Namespaces?
1) To fixed the name collisions between your code and internal PHP classes/functions/constants or third-party classes/functions/constants.
2) Ability to shorten for Extra_Long_Names, improving readability of source code.



Question: Explain about Namespaces in PHP7?
  1. Namespace must be the first statement in the PHP Script.
  2. We can define Sub-Namespace also
  3. Defining multiple namespaces in same file with/without using bracketed syntax (Multiple declaration is not Recommended)
  4. We can access the Global method of namespaces using \
    namespace A\B\C;
            function strlen($string){
                return \strlen($string).'--arun';
            }
        echo strlen('imarun');
  5. we can use __NAMESPACE__ for getting the current namespace name
  6. We can use Alias for namespace as below
    use mynamespace\car as mycar;
  7. import multiple namespaces in single line
    use some\namespace\{ClassA, ClassB, ClassC as C};
            use function some\namespace\{fn_a, fn_b, fn_c};
            use const some\namespace\{ConstA, ConstB, ConstC};  
  8. Using namespaces: fallback to global function/constant
    a) An unqualified class name resolve to current namespace
    b) An unqualified function name resolve to current namespace
    c) An unqualified constant name resolve to Global namespace



Question: What is Class Constants?
Class Constants: You can define constant as string OR expression. but you can not define as variable, or function.


Question: When autoloading classes loads automatic?
a) Autoload when create object.
b) Autoload when include an another (using extends)
c) Autoload when include an interface (using implements )
d) we Can add exception in spl_autoload_register


Question: What is Visibility?
Class methods must be declare as public, protected or private.
You can also declare with var which means public


Question: What are Static Keyword?
Declaring class property or methods as static makes them accessible without needing an instantiation of the class.
$this is not available inside the function.




Thursday 10 January 2019

PHP namespace example - Tutorial

PHP namespace example - Tutorial

Question: What is namespace in PHP?
namespace is reserve keywords which is used to encapsulating the class, method, data member variables and static methods.


Question: Benefits of namespace in PHP?
  1. Encapsulate the data which fixed collisions between code you create, and internal PHP classes/functions/constants.
  2. Ability to alias which improved the readability of source code.



Question: Which PHP Version support namespaces?
PHP5.3



Question: How namespaces are defined?
namespaces are defined with keyword namespace
For Example:
namespace math



Question: Example - Simple class without namespace (OR Global namespace)?
File: /myproject/simple-file.php
class mathSimple {

    function add($a, $b) {
        return $a + $b;
    }

}

File: /myproject/run-example.php
include "simple-file.php";

$obj1 = new mathSimple(); //calling for Global Namespace
echo $obj1->add(10,20);//30

When we execute run-example.php file, it will return 30


Question: Example - Simple class with namespace?
File: /myproject/namespace-file.php
namespace math;

class mathSimple {

    function add() {        
        return array_sum(func_get_args()) ;
    }

}

File: /myproject/run-example.php
namespace math;
include "simple-file.php";
include "namespace-file.php";


$obj1 = new \mathSimple(); //calling for Global Namespace
echo $obj1->add(10,20,30);

echo "\n";
$obj2 = new mathSimple(); //calling for namespace math
echo $obj2->add(10,20,30);
Question: How to import namespaces?
use Zend\View\Model\ViewModel;



Question: How to use alias namespaces for import namespace?
use Zend\View\Model\ViewModel as ZendViewModel;



Question: Can we override the php in-built using namespace?
Yes, We can do. File: /myproject/namespace-file2.php
namespace math;

class mathSimple {

    function add() {        
        return array_sum(func_get_args()) ;
    }

}
function strlen($string){
    return 'in built functi';
}


File: /myproject/run-example2.php
namespace math;
include "namespace-file2.php";

echo strlen('string'); //calling namespace function
echo \strlen('strlen'); //calling inbuilt function