Saturday, December 9, 2017

Interview Questions for PHP

Dependency injection is a technique whereby one object supplies the dependencies of another object. A dependency is an object that can be used (a service). An injection is the passing of a dependency to a dependent object (a client) that would use it.

Type Hinting in PHP. Since PHP 5 you can use type hinting to specify the expected data type of an argument in a function declaration. When you call the function, PHP will check whether or not the arguments are of the specified type. If not, the run-time will raise an error and execution will be halted.

Getters and Setters are object methods that allow you to control access to a certain class variables / properties. Sometimes, these functions are referred to as “mutator methods”. A “getter” allows to you to retrieve or “get” a given property. A “setter” allows you to “set” the value of a given property.

Final

The methods or classes can not be modified by a child class. This prevents class inheritance, method-overriding and/or redefinition of methods.
Only class definitions and/or methods inside a class can be defined as final.

Static

Declares class methods or properties as a static value so that you have access to them without instantiating an object. These are shared between parent and child-classes.
A class definition can not be static unlike final.

The size of all cookies should not exceed 4KB.
You can store as much data as you like within in sessions. All sessions are stored on the server. The only limits you can reach is the maximum memory a script can consume at one time, which by default is 128MB.



What is memcache?
Free & open source, high-performance, distributed memory object caching system
Memcache is an in-memory key – value pair’s data store (data store in the memory in the form of key - value)

Pros:
  • Data store in memory so its mush faster.
  • Reduce database load.
  • Perfect for website with database load.
  • Reduce the number of request to database
Cons:
  • Not a persistent store
  •  Cant cache large objects (Max value 1 MB)

What is Serialization?

Serialization Is the process of converting an object into a stream in the form bytes.
Deserialization is the process of reconstructing an object that has been serialized before.


What is SQL injection?

SQL injection attack occurs when a user provides SQL code as user input for a Web page, and the SQL code is then executed in the database.

Using prepared statement and parameterized queries we can prevent SQL injection.

$pdo = new PDO('mysql:hostname','databsename','user','psw');
$name = $_GET['name'];
$stmt = $pdo->prepare('SELECT * FROM employees WHERE name = :name');
$stmt->execute(array('name' => $name));
foreach ($stmt as $row) {
    // do something with $row
}


What is Store Procedure?
A stored procedure is a named collection of SQL statements and procedural logic i.e, compiled, verified and stored in the server database. A stored procedure is typically treated like other database objects and controlled through server security mechanism.

Instead of writing code in PHP we can write stored procedure/functions as those are stored in compiled format those executes faster than PHP codes.


DELIMITER //
CREATE PROCEDURE `employee_pro2`(IN `empemail` VARCHAR(50), IN `empname` VARCHAR(50))

BEGIN
SELECT employee_id, name, email_address FROM employee WHERE email_address = empemail and name = empname;
END
// DELIMITER ;


DROP PROCEDURE employee_pro4


What is difference between session and cookies?

In Session data store on server and Cookies data are store on client side.
Session data are more secure because they never travel on every http request and Cookies data are less secure because they travel with each and every HTTP request.
You can store objects in session (Store large amount of data) and you can store only string type of data in cookies (file size - 4kb).

Session cannot be used for future reference and Cookies are mostly used for future references.

OOPS Features Supported by PHP

  •  Class
  •  Object
  • Constructor and Destructor
  • Inheritance
  • Encapsulation

Class
The class is a programming block structure contains its own properties and methods to process the domain functionalities. We can create instances to a class to access its properties and method.

Object
Objects or Instances can be created for a class to access the class components.

Constructor and Destructor
The Constructor is a class function called automatically when an instance is created for that class. The Destructor is a function that is invoked when the class instance is destroyed.

Inheritance
There are three types of inheritance like single, multiple and multi-level inheritance. PHP supports single and multi-level inheritance. It will not support multiple inheritances. That is, A class can extend only one parent class. To overcome this limitation, PHP provides the Traits concept which allows us to use functions of one class into one or more classes.

Encapsulation
Since properties and methods of the objects are enclosed with the class container in an abstract manner, any code from outside cannot access the class, unless otherwise by using objects of that class. In this way, the properties are encapsulated.



The differences between the persistent cookie and temporary cookie are given below:

A persistent cookie is a cookie which is stored in a cookie file permanently on the browser's computer. By default, cookies are created as temporary cookies which stored only in the browser's memory. When the browser is closed, temporary cookies will be erased. You should decide when to use temporary cookies and when to use persistent cookies based on their differences:

Temporary cookies cannot be used for tracking long-term information.

Persistent cookies can be used for tracking long-term information.

Temporary cookies are safer because no programs other than the browser can access them.
Persistent cookies are less secure because users can open cookie files see the cookie values.

Wednesday, December 6, 2017

Objective Q & A - PHP/MySql

Useful Links - Codeigniter

Differance between PHP 5 & PHP 7

PHP 7

1. Speed
Refactor the PHP codebase in order to reduce memory consumption and increase performance.

2. Type Declarations
. Scalar Type Hints
. Return Type Declarations

3. Error Handling
Most of the fatal error converted in to Exception error.
Fatal errors still exist for certain conditions, such as running out of memory, and still behave as before by immediately stopping the script. An uncaught exception will also continue to be a fatal error in PHP 7
warnings and notices remain unchanged in PHP 7.
In PHP 7, Error and Exception both implement the new Throwable class. What that means is that they basically work the same way. And also, you can now use Throwable in try/catch blocks to catch both Exception and Error objects.
Virtually all errors in PHP 5 that were fatal, now throw instances of Error in PHP 7.

4. New Operators
. spaceship operator, or Combined Comparison Operator(<=>)
. Null Coalesce Operator ( $name = $firstName ??  "Guest"; )