How can we declare cookie in PHP and then how can we expire it ?

setcookie() function is using to set cookies in php
To declare Cookies syntax will be:- setcookie(name, value, expire, path, domain);
name : Name of the cookie
value : Value of the cookie
expire : The time for cookie to expire
path : path to save the cookie where we want to save the cookie information
domain : domain name on which we want to save the cookie
eg : setcookie(“username”,”kiran”,time()+60*60*60*24);
In the above example the cookie name is username having value “kiran” and set for one day
To expire cookies we have set the time of cookie in past
To expire Cookies syntax will be: setcookie(name,value,time-3600);

What is str_replace()

This function replace some characters with some other characters in a string , this function is case sensitive
syntax:- str_replace(find,replace,string);
find:-required,specifies the value to find
replace:-required,specifies the value to replace the value in find
string:-required,specifies the string to searched

str_replace()
<?php

echo str_replace(“world”,”india”,”hello world”);

?>

OUTPUT :
hello india

What is Final Keyword in PHP ?

PHP 5 introduces the final keyword, which prevents child classes from overriding a method by prefixing the definition with final. If the class itself is being defined final then it cannot be extended.

Final methods example
<?php

class BaseClass {
public function test() {
echo “BaseClass::test() called\n”;
}

final public function moreTesting() {
echo “BaseClass::moreTesting() called\n”;
}
}

class ChildClass extends BaseClass {
public function moreTesting() {
echo “ChildClass::moreTesting() called\n”;
}
}

?>

OUTPUT :
Results in Fatal error: Cannot override final method BaseClass::moreTesting()

Final class example
<?php

final class BaseClass {
public function test() {
echo “BaseClass::test() called\n”;
}

// Here it doesn’t matter if you specify the function as final or not
final public function moreTesting() {
echo “BaseClass::moreTesting() called\n”;
}
}

class ChildClass extends BaseClass {
}

?>

OUTPUT :
Results in Fatal error: Class ChildClass may not inherit from final class (BaseClass)

What is difference between abstract class and interface ?

An interface does not contain any code,it contain only declaration of methods,properties,events. Interfaces allow us to code which specifies which methods a class must have implement. Interfaces defines with the word interface. All methods in interfaces must be public.

interface SomeInterface
{
public function doSomething( $variable );
}

Abstract classes are look like interfaces. Abstract classes may contain code although it may also have abstract method that do not have code. Abstract classes defines only signature of the method ,not implementation. The child class which inherit the abstract class must define all the abstract methods of its parent class.

abstract class SomeInterface
{
abstract protected function doSomething( $variable )
{
//Put some code here if you like
}
}

Difference between Interfaces and Abstract classes as follows.

INTERFACE
1) A class can implement several interfaces.
2) An interface can not provide any code at all.
3) Interface constants work exactly like class constants, however, they can not be overridden.
4) Interfaces can easily be added to a third party class.
5) Interfaces describe the abilities of a class, and not the identity of the class (It defines what the class can do).
6) All declared methods MUST be public.
7) If various implementations share the same method signatures, the an interface should be used. If subclasses are going to different (heterogeneous), then use interfaces.
8 ) Interfaces are almost double as slow as abstract classes.
9) Interfaces are typically not designed to share.
10) If you add a new method to an interface, then you MUST update all implementations of the interface and add code to implement the new method.

ABSTRACT CLASS
1) A class can only extend one abstract class.
2) An abstract class can provide default code, and parts that can be overridden.
3) Abstract classes can have their constants overridden by extending classes.
4) Third party classes might have to be rewritten to extend the abstract class.
5) An abstract class defines the code identity of the descendants. (It defines what the class is). For example: If you defined a Dog as abstract, then Shepherds are descendants of Dogs.
6) Methods can be public or protected. If the abstract class defines a method as protected, then it can be declared as protected or public in the sub-class.
7) If various implementations share a common “kind” or behavior, the an abstract class should be used. For example, if sub-classes are going to be similar (homogeneous), use abstract classes.
8 ) Pretty fast.
9) Shared code can be put in abstract classes.
10) If a new method is added to a class, you can provide default implementation for it. All existing code will continue to work without change.

What is the difference between explode (); and split(); methods ?

Both the functions are used to Split a string.

The split() function splits the string into an array using a regular expression and returns an array
eg : $array = split(“\.”,”123.456.789.000″);
print_r($array);
OUTPUT : Array ( [0] => 123 [1] => 456 [2] => 789 [3] => 000 )

Explode() function is used to split a string using another string
eg : $array = explode(“.”,”123.456.789.000″);
print_r($array);
OUTPUT : Array ( [0] => 123 [1] => 456 [2] => 789 [3] => 000 )