1. Define cunstructor & example
2. Introspection
3. Clone object in php
4 marks
4. Diff constructor & distructor
5. Types of cunstructor & advantages
6.single inheritance & example
7. Diff $post & $get
8. Define cookies and how to create cookies in php
9.diff session & cookies
10. Features of php
2. Introspection
>
offers the useful ability to examine an object's characteristics, such as its name, parent class (if any) properties, classes, interfaces, and methods.
3. Clone object in php
>
object copy is created by using the clone keyword (which calls the object's __clone() method if possible). $copy_of_object = clone $object; When an object is cloned, PHP will perform a shallow copy of all of the object's properties. Any properties that are references to other variables will remain references.
4. Diff constructor & distructor
>
Constructors are the very basic building blocks that define the future object and its nature. You can say that the Constructors are the blueprints for object creation providing values for member functions and member variables.
Once the object is initialized, the constructor is automatically called. Destructors are for destroying objects and automatically called at the end of execution.
In this article, we are going to learn about object-oriented concepts of constructors and destructors.
Both are special member functions of any class with different concepts but the same name except destructors are preceded by a ~ Tilda operator.
Syntax:
__construct():
function __construct()
{
// initialize the object and its properties by assigning
//values
}
__destruct():
function __destruct()
{
// destroying the object or clean up resources here
}
Note: The constructor is defined in the public section of the Class. Even the values to properties of the class are set by Constructors.
Constructor types:
Default Constructor:It has no parameters, but the values to the default constructor can be passed dynamically.
Parameterized Constructor: It takes the parameters, and also you can pass different values to the data members.
Copy Constructor: It accepts the address of the other objects as a parameter.
Inheritance: As Inheritance is an object-oriented concept, the Constructors are inherited from parent class to child class derived from it. Whenever the child class has constructor and destructor of their own, these are called in order of priority or preference.
Pre-defined Default Constructor: By using function __construct(), you can define a constructor.
Note: In the case of Pre-defined Constructor(__construct) and user-defined constructor in the same class, the Pre-defined Constructor becomes Constructor while user-defined constructor becomes the normal method.
Program:
php
class Tree
{
function Tree()
{
echo "Its a User-defined Constructor of the class Tree";
}
function __construct()
{
echo "Its a Pre-defined Constructor of the class Tree";
}
}
$obj= new Tree();
?>
Output:
Its a Pre-defined Constructor of the class Tree
Parameterized Constructor: The constructor of the class accepts arguments or parameters.
The -> operator is used to set value for the variables. In the constructor method, you can assign values to the variables during object creation.
Program:
php
class Employee
{
Public $name;
Public $position;
function __construct($name,$position)
{
// This is initializing the class properties
$this->name=$name;
$this->profile=$position;
}
function show_details()
{
echo $this->name." : ";
echo "Your position is ".$this->profile."\n";
}
}
$employee_obj= new Employee("Rakesh","developer");
$employee_obj->show_details();
$employee2= new Employee("Vikas","Manager");
$employee2->show_details();
?>
Output:
Rakesh : Your position is developer
Vikas : Your position is Manager
Constructors start with two underscores and generally look like normal PHP functions. Sometimes these constructors are called as magic functions starting with two underscores and with some extra functionality than normal methods. After creating an object of some class that includes constructor, the content of constructor will be automatically executed.
Note: If the PHP Class has a constructor, then at the time of object creation, the constructor of the class is called. The constructors have no Return Type, so they do not return anything not even void.
Advantages of using Constructors:
Constructors provides the ability to pass parameters which are helpful in automatic initialization of the member variables during creation time .
The Constructors can have as many parameters as required and they can be defined with the default arguments.
They encourage re-usability avoiding re-initializing whenever instance of the class is created .
You can start session in constructor method so that you don't have to start in all the functions everytime.
They can call class member methods and functions.
They can call other Constructors even from Parent class.
Note : The __construct() method always have the public visibility factor.
Program:
php
class ParentClass
{
function __construct()
{
print "Parent class constructor.\n";
}
}
class ChildClass extends Parentclass
{
function __construct()
{
parent::__construct();
print "Child Class constructor";
}
}
$obj = new ParentClass();
$obj = new ChildClass();
?>
Output
Parent class constructor.
Parent class constructor.
Child Class constructor
Note: Whenever child class object is created, the constructor of subclass will be automatically called.
Destructor: Destructor is also a special member function which is exactly the reverse of constructor method and is called when an instance of the class is deleted from the memory. Destructors (__destruct ( void): void) are methods which are called when there is no reference to any object of the class or goes out of scope or about to release explicitly.
They don't have any types or return value. It is just called before de-allocating memory for an object or during the finish of execution of PHP scripts or as soon as the execution control leaves the block.
Global objects are destroyed when the full script or code terminates. Cleaning up of resources before memory release or closing of files takes place in the destructor method, whenever they are no longer needed in the code. The automatic destruction of class objects is handled by PHP Garbage Collector.
~ ClassName()
{
}
Note: The destructor method is called when the PHP code is executed completely by its last line by using PHP exit() or die() functions.
Program:
php
class SomeClass
{
function __construct()
{
echo "In constructor, ";
$this->name = "Class object! ";
}
function __destruct()
{
echo "destroying " . $this->name . "\n";
}
}
$obj = new Someclass();
?>
Output:
5. Types of cunstructor & advantages
>
Constructor types:
Default Constructor:It has no parameters, but the values to the default constructor can be passed dynamically.
Parameterized Constructor: It takes the parameters, and also you can pass different values to the data members.
Copy Constructor: It accepts the address of the other objects as a parameter.
-
Advantage :
Constructors provides the ability to pass parameters which are helpful in automatic initialization of the member variables during creation time .
The Constructors can have as many parameters as required and they can be defined with the default arguments.
They encourage re-usability avoiding re-initializing whenever instance of the class is created .
You can start session in constructor method so that you don't have to start in all the functions everytime.
They can call class member methods and functions.
They can call other Constructors even from Parent class.
6.single inheritance & example
>
PHP supports Single inheritance. Single inheritance is a concept in PHP in which one class can be inherited by a single class only. We need to have two classes in between this process. One is the base class (parent class), and the other a child class itself
--
class MyAccess {
var $var = "This is first var";
protected $fist_name;
// simple class method
function returnVar() {
echo $this->fist_name;
}
function set_fist_name($set_this){
$this->fist_name = $set_this;
}
}
class child extends MyAccess {
function setVal($set_this){
$this->fist_name = $set_this;
}
function getVal(){
echo $this->fist_name;
}
}
$obj1 = new child();
$obj1->setVal("Jai Shre");
$obj1->getVal();
?>
MyAccess is the parent, and the child is the name of the child class.
Output:
Jai shre
7. Diff $post & $get
>
GET vs POST Method in PHP
GET is a method that sends information by appending them to the page request. POST is a method that transfers information via HTTP header.
URL
The form information is visible in the URL The form information is not visible in the URL
Information Amount
Limited amount of information is sent. It is less than 1500 characters. Unlimited amount of information is sent.
Usage
Helps to send non-sensitive data Helps to send sensitive data (passwords), binary data (word documents, images)and uploading files
Security
Not very secure. More secure.
Bookmarking the Page
Possible to bookmark the page Not possible to bookmark the page
9. Define cookies and how to create cookies in php
>
A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer.
Each time the same computer requests a page with a browser, it will send the cookie too. With PHP, you can both create and retrieve cookie values.
Create Cookies With PHP
A cookie is created with the setcookie() function.
Syntax
setcookie(name, value, expire, path, domain, secure, http only);
Only the name parameter is required. All other parameters are optional
10.diff session & cookies
>
Cookie
Session
Cookies are client-side files on a local computer that hold user information. Sessions are server-side files that contain user data.
Cookies end on the lifetime set by the user. When the user quits the browser or logs out of the programmed, the session is over.
It can only store a certain amount of info. It can hold an indefinite quantity of data.
The browser's cookies have a maximum capacity of 4 KB. We can keep as much data as we like within a session, however there is a maximum memory restriction of 128 MB that a script may consume at one time.
Because cookies are kept on the local computer, we don't need to run a function to start them. To begin the session, we must use the session start() method.
Cookies are not secured. Session are more secured compare than cookies.
Cookies stored data in text file. Session save data in encrypted form.
11. Features of php
There are some important features of PHP given below:
Performance:
Open Source:
Familiarity with syntax:
Embedded:
Platform Independent:
Database Support:
Error Reporting -
Loosely Typed Language: