Showing posts with label oop. Show all posts
Showing posts with label oop. Show all posts

Serialization and UnSerialization in PHP

Serialization/UnSerialization:

  • Generates a storable representation of a value.
  • serialize() returns a string containing a byte-stream representation of any value that can be stored in PHP. 
  • unserialize() can use this string to recreate the original variable values.
  • This process makes a storable representation of a value that is useful for storing or passing PHP values.
  • To make the serialized string into a PHP value again, use unserialize().

Before starting your serialization process, PHP will execute the __sleep function automatically. This is a magic function or method.
Before starting your unserialization process, PHP will execute the __wakeup function automatically. This is a magic function or method.


What can you Serialize and Unserialize?
  • Variables
  • Arrays
  • Objects 
What cannot you Serialize and Unserialize?
  • Resource-type
Example:
//BaseClass.php
<?php

  class BaseClass {
      public $var = 100;
    
      public function show() {
          echo $this->var;
      }
  }
  
// test1.php:


  include("BaseClass.php");
  
  $a = new A;
  $v = serialize($a);
  file_put_contents('store_in_var', $v);


// page2.php:
  
  include("BaseClass.php");
  $v = file_get_contents('store_in_var');
  $a = unserialize($v);




  $a->show();
?>

In the Case if you want to store entire array into database then with serialze() it would be useful to store an entire array in a field in a database.
You can pass an array to the function, and it will return a string that is essentially the string. 
You can then unserialize() it to obtain the full array once again.


example:


<?php

$str_array = array( "Rajesh Sharma", "Rajesh", "RajeshS" );
$serialized_str = serialize($str_array);
echo $serialized_str;


?>


This will output a:3:{i:0;s:14:"Rajesh Sharma";i:1;s:6:"Rajesh";i:2;s:7:"RajeshS";}
so you can store entire array in a field in a database.


<?php

$str_array = array( "Rajesh Sharma", "Rajesh", "RajeshS" );
$serialized_str = serialize($str_array);


print_r(unserialize($serialized_str));


?>
This will output a Array ( [0] => Rajesh Sharma[1] => Rajesh [2] => RajeshS ).


Bookmark and Share

Explain about Abstraction?

In this tutorial ,I will explain about abstract class in PHP, how to declare and use of an abstract class.

Abstract Class:
It may contain one or more abstract methods.abstract classes may not be instantiated.The child classes which inherits the property of abstract base class, must define all the methods declared as abstract.
Any class that contains at least one abstract method must also be abstract.
if the abstract method is defined as protected, the function implementation must be defined as either protected or public, but not private.

Example:

<?php
abstract class AbstractClass
{
    abstract protected function getValue();
    abstract protected function setValue($val);

    public function Display() {
        print $this->getValue() . "\n";
    }
}

class ChildClass1 extends AbstractClass
{
    protected function getValue() {
        return "ChildClass1";
    }

    public function setValue($val) {
        return "ChildClass{$val}";
    }
}

class ChildClass2 extends AbstractClass
{
    public function getValue() {
        return "ChildClass2";
    }

    public function setValue($val) {
        return "ChildClass{$val}";
    }
}

$class1 = new ChildClass1;
$class1->Display();
echo "<br>";
echo $class1->setValue('1') ."\n";
echo "<br>";
$class2 = new ChildClass2;
$class2->Display();
echo "<br>";
echo $class2->setValue('2') ."\n";
?>


Bookmark and Share

What are different properties provided by Object-oriented systems ?

What are different properties provided by Object-oriented systems ?Can you explain different properties of Object Oriented Systems? Whats difference between Association , Aggregation and Inheritance relationships?
Properties of Object Oriented Systems:
  • support inheritance
  • provides encapsulation of data
  • provides extensibility of existing data types and classes
  • provide support for complex data types
  • Inheritance-one class inherite the property of another class..
  • Aggregation-.is a part whole relationship.
  • Association.:is a relationship between 1 or more instances of a class.





Bookmark and Share

What is the relation between Classes and Objects

What is the relation between Classes and Objects.Class is a definition, while object is a instance of the class created
What is the relation between Classes and Objects

Class is a definition, while object is a instance of the class created. 
class is like a blueprint/template in OOPs, and this template is used to create objects.The collection of properties & behavior of an object is also called as class.
A class contains  properties, fields, data members, attributes.


Example:

Car is an object , cars is a class
            public class cars
            {
                  //
            }


According to the sample given below we can say that the cars object, named object_car, has created out of the cars class.

               cars object_car = new cars();

object is an instance of a class.An object is an entity that has attributes, behavior, and identity. Objects are members of a class.
Objects are accessed, created or deleted during program run-time.

Declaration of an Object in OOPs
ClassName objectName=new ClassName();
Example:
          Car objCar= new Car();



Attribute in OOPs: Attributes define the characteristics of a class. In Class Program attribute can be a string or it can be a integer. 
Behavior in OOPS:  Every object has behavior.
Identity in OOPS:  Each time an object is created the object identity is been defined.



Bookmark and Share

What is an Object ?

what is Object in Object oriented programming.object is an instance of a class.An object is an entity that has attributes, behavior, and identity. Objects are members of a class.Object in OOP, PHP, Question, what is an object,com object,object,objective,object oriented,object oriented,object oriented programing,OOP

Object in Object oriented programming

object is an instance of a class.An object is an entity that has attributes, behavior, and identity. Objects are members of a class.
Objects are accessed, created or deleted during program run-time.


Declaration of an Object in OOPs
ClassName objectName=new ClassName();


Example:
Car objCar= new Car();

Attribute in OOPs: Attributes define the characteristics of a class. In Class Program attribute can be a string or it can be a integer. 
Behavior in OOPS:  Every object has behavior.
Identity in OOPS:  Each time an object is created the object identity is been defined.


Bookmark and Share

Advantages of object oriented programming?

Advantages of object oriented programming,Code Re-usability,Code extensibility,Maintainability,Modularity,Modifiability

Advantages of object oriented programming:

  • Code Re-usability(Polymorphism,Interfaces): In OOPs objects created in one program can be reused in different programs.
  • Code extensibility: for addding new features or modifying existing objects can be solved by introducing new objects.
  • Catch errors at compile time rather than at runtime.
  • Maintainability :objects can be maintained separately, so u can easily fix errors.
  • Imrove error handling:We can use exceptions to improve error handling.
  • Modularity: can create seperate Modules.
  • Modifiability:it is easy to make minor changes in some classes as Changes inside a class do not affect any other part of a program.


Bookmark and Share

What is Object Oriented Programming?

Introduction to Object-Oriented Programming Using PHP.This page will describes the term `object-oriented programming`.let's start with few basic concepts before you can begin writing any code.<br /> Object-oriented programming is a method of programming based on a hierarchy of classes, and well-defined objects

Object Oriented Programming

Introduction to Object-Oriented Programming Using PHP.This page  will describes the term `object-oriented programming`.let's start with few basic concepts before you can begin writing any code.
Object-oriented programming is a method of programming based on a hierarchy of classes, and well-defined objects.


Brief overview of object-oriented concepts and terminology:


Bookmark and Share