Wednesday 20 December 2017

Encapsulation in PHP (OOP)

Encapsulation in PHP (OOP)

Question: What is Encapsulation?
The wrapping up of "Data member" and "Member functions" into a single unit (called class) is known as encapsulation.


Question: Describe the Encapsulation?
Encapsulation means hiding the internal details of an object.
OR
Encapsulation is a technique used to protect the information in an object from the other object.


Question: Give Example of the Encapsulation?
class Arithmetic {
    private $first = 10;
    private $second = 50;

    function add() {
        return $this->first + $this->second;
    }

    function multiply() {
        return $this->first * $this->second;
    }

}

//Create the object 
$obj = new Arithmetic( );

//Add Two number
echo $obj->add(); //60
//multiply Two number
echo $obj->multiply(); //500


Friday 10 November 2017

How to replace double quoted string with bold


Question: How to replace double quoted string with bold in PHP?

$description='Hello "dude", how are you?';
echo preg_replace('/"([^"]+)"/', '<strong>$1</strong>', $description);


Output
Hello dude, how are you?




Question: How to replace bracket string with italic in PHP?

$description='Hello (dude), how are you?';
echo preg_replace('/\(([^)]+)\)/', '<i>$1</i>', $description);


Output
Hello dude, how are you?