Author: AdminIN

Let’s Some Artisan commands

  1. php artisan controller:make ControllerName – this code will generate boilerplate code for a controller
  2. php artisan list – this command will list available artisan commands
  3. php artisan routes – this command will list all the registered routes
  4. php artisan down – this command will activate the maintenance mode and your website/application will display a custom message and deny access to other
  5. php artisan up – this command will disable the maintenance mode of your website/application
  6. php artisan test – this command will run PHPUnit tests
  7. php artisan migrate – this command runs the database’s migrations
  8. php artisan view: publish – this command publishes a package’s views to the application
  9. php artisan db:seed – this command seeds records to the database
  10. php artisan config:publish – this command publishes a package’s configuration to the application

OOP in PHP

OOP in PHP

PHP 5 (although most of the ideas in this article also apply to PHP 4) has great support for object oriented programming by providing easy class creation. PHP provides every paradigm other “true” OOP languages implement (Python and JAVA, for example), like inheritance, polymorphism and encapsulation.


Inheritance

The basic idea behind inheritance is that similar objects share common properties. So by creating a “generic” class, we can have a blueprint to build our subsequent classes on. Imagine, if you will, a car’s properties: color, number of wheels, horsepower, number of seats, etc. Having this template we can further specialize our cars by extending this class: creating a racing car that has a “nitro” property, or a truck that has a “trailer” property. The bottom line is: create a more generic class that contains most of the common attributes and you will have much less work defining other objects only slightly different. Instead of rewriting the whole code, you just extend it’s properties, saving a lot of time in the process.


Encapsulation

As previously explained, one of the main advantages of using objects is that we don’t need to reveal all of its members (attributes or functions); just the necessary interfaces to work with it. Details not useful to the use of these objects should be hidden from the rest of the objects. This is what is referred to as encapsulation.

Levels of visibility

  • public: means that a class member is visible and usable / modifiable by everyone
  • private: means that a class member is only usable / modifiable by the class itself
  • protected: means that a class member is only usable / modifiable by the class itself and eventual sub-classes

Polymorphism

Polymorphism is an OOP characteristic that allows the programmer to assign a different meaning or usage to something in different contexts – specifically, to allow a class member to perform different tasks depending on the context it was used. Imagine you have a Person class and two sub-classes of Person: Japanese and American. Both implement a function named talk(), but with different languages and social context. And while both of them are basically People (as they derive from the class Person), their implementation of the function talk() is very different. So you basically have two objects of the class Person in which the talk() function works differently.

PHP OOPS

Q.Suppose that you have to implement a class named Dragonball. This class must have an attribute named ballCount (which starts from 0) and a method iFoundaBall. When iFoundaBall is called, ballCount is increased by one. If the value of ballCount is equal to seven, then the message You can ask your wish is printed, and ballCount is reset to 0. How would you implement this class?

class dragonBall{
  private $ballCount;

  public function __construct(){
    $this->ballCount=0;
  }

  public function iFoundaBall(){
    $this->ballCount++;
    if($this->ballCount===7){
      echo "You can ask for your wish.";
      $this->ballCount=0;
    }
  }
}



What is meant by PEAR in PHP?

PEAR is an acronym for “PHP Extension and Application Repository” The purpose of PEAR is to provide:

  • A structured library of open-sourced code for PHP users
  • A system for code distribution and package maintenance
  • A standard style for writing code in PHP
  • PHP Foundation Classes (PFC)
  • PHP Extension Community Library (PECL)

PHP MAGIC METHODS?

Magic methods are member functions that are available to all the instance of class.
Magic methods always start with “__”. Eg. __construct.

All magic methods need to be declared as public
To use a method, it should be defined within the class or program scope
Various Magic Methods used in PHP 5 are:
__construct()
__destruct()
__set()
__get()
__call()
__toString()
__sleep()
__wakeup()
__isset()
__unset()
__autoload()
__clone().

How can we increase the execution time of a PHP script?

Default time allowed for the PHP scripts to execute is 30 secs mentioned in the php.ini file. The function used is set_time_limit(int sec). If the value passed is ‘0’, it takes unlimited time. It should be noted that if the default timer is set to 30 sec, and 20 sec is specified in set_time_limit(), the script will run for 45 seconds.

This time can be increased by modifying the max_execution_time in secs. The time must be changed keeping the environment of the server. This is because modifying the execution time will affect all the sites hosted by the server.

The script execution time can be increased by

  • Using sleep() function in PHP script
  • Using set_time_limit() function
  • The default limit is 30 seconds. The time limit can be set to zero to impose no time limit to pause.

Stored Procedure

A stored procedure is a set of Structured Query Language (SQL) statements with an assigned name, which are stored in a relational database management system as a group, so it can be reused and shared by multiple programs.
Stored procedures can access or modify data in a database, but it is not tied to a specific database or object, which offers a number of advantages.

Benefits of using stored procedures

A stored procedure provides an important layer of security between the user interface and the database. It supports security through data access controls because end users may enter or change data, but do not write procedures. A stored procedure preserves data integrity because information is entered in a consistent manner. It improves productivity because statements in a stored procedure only must be written once.

Use of stored procedures can reduce network traffic between clients and servers, because the commands are executed as a single batch of code. This means only the call to execute the procedure is sent over a network, instead of every single line of code being sent individually.

Stored procedure vs. function

Stored procedures and functions can be used to accomplish the same task. Both can be custom-defined as part of any application, but functions are designed to send their output to a query or T-SQL statement. Stored procedures are designed to return outputs to the application, while a user-defined function returns table variables and cannot change the server environment or operating system environment.

CURL

$postData = array(
   "site" => "web technology experts notes",
   "dailyuser" => "1000",
   "location" => "India"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.example.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$output = curl_exec($ch);
curl_close($ch);
echo $output;

Mysql Database Connection Using PDO?

DB Connection Using PDO

$host = "localhost";
$username = "root";
$password = "";
 $conn = new PDO("mysql:host=$host;dbname=myDB", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";