what is oops in php?

oops in php

What is OOPs?


OOPs is a concept or mechanism. which is used for developing the application software.

these concept are :-

  • Class
  • Object
  • Polymorphism
  • Enacpsulation
  • Inheritence
  • Abstraction

  • Class :-

  • In OOPs, Everything start with classes. Classes are the type of objects, and the object is an instance of the class.
  • class is the template or blue print of the object. That describe the details of an object.
  • In php class is created by class keyword.
  • Class contain the variable and function.
  • In php class Name start with a capital letter and objects are given name that start with a lowercase letter.
  • As you know, classes can hold data items, called properties.
  • In the class you are declaring the properties and methods that will go into the objects of this class. And that means you use the var statement to declare properties in php.
  • File name : index.php

    Class Person
    {
    var $name;
    function set_name($data)
    {

    // statements....
    }
    }

    Now you have got to store the name passed to the set_name function in the $name variable. You could do that, by accessing $name as a global variable. Then you can assign the agrument passed to set_name , $data, to the internally stored name, $name, like that

    File name : index.php

    class Person
    {
    var $name;
    function set_name($data)
    {
    global $name;
    $name = $data;
    }
    }

    That store the person?s name. you will also need some way to read the person?s name, so you might add another method called get_name.

    File name : index.php

    class Person
    {
    var $name;
    function set_name($data)
    {
    global $name;
    $name = $data;
    }
    function get_name()
    {
    global $name;
    return $name;
    }
    }

    You won?t normally see OOP done using the globle keyword. Instead, you usually refer to the properties to the class using the $this keyword. The $this keyword points to the current object. in php terms, this :
    Global $name;
    $name = $data;
    Can be replaced by this way :
    $this->name = $data;
    NOTE :- $this is followed by the -> operator.


    Object

    When you use the class to build an object this process is known as instantiation. Instantiating an object requires two things:

  • A memory location into which to load the object. This is automatically handled for you by php.
  • The data that will populate the values of the properties. This data can come from a database, or any text file.
  • File name : index.php

    <?php
    class Demo
    {
    // data member
    // member function
    }

    ?>

    You can instantiate an object of type Demo class.
    <?php
    require_once(?class.Demo.php?);
    $obj = new Demo();
    ?>
    Here $obj is the object of the Demo class.

    Adding a method in class :-

    File name : index.php

    <?php
    class Demo
    {
    function sayhello(
    {
    echo "hi mahtab";
    }
    }

    $obj = new Demo();
    $obj->sayhello();
    ?>

    File name : index.php

    <?php
    class Demo
    {
    function sayhello()
    {
    echo "hi mahtab";
    }
    }
    ?>

    <?php
    $obj = new Demo();
    $obj->sayhello();
    ?>


    Output :-
    Hi mahtab


    File name : index.php

    <?php
    class Demo
    {
    function sayhello()
    {
    echo "hi mahtab";
    }
    $obj = new Demo();// here error occurs, we can't create the object under the body of class.
    $obj->sayhello(); // error
    }
    ?>
    Note :-
    Echo and print funtion use only within function and outside the class.

    Parametrized function

    File name : index.php

    <?php
    class Demo
    {
    function sayhello($name)
    {
    echo "hi $name";
    }
    }
    $obj = new Demo();
    $obj->sayhello('mahtab_habib');
    ?>
    Output :-
    Hi mahtab_habib

    File name : index.php

    NOTE :-

  • The -> operator is used to access all methods and properties of an objects.
  • Php does not use the dot (.) operator in its oop syntax at all.

  • You simply declare a variable inside the class to hold the value of the property. This variable is declared at the top of the class declaration, inside the braces that bracket the class's code. The name of the variable is the name of the property. If the variable is called $color, you will have a property called color.

    File name : index.php

    <?php
    class Demo
    {
    public $name;
    function sayhello()
    {
    print "Hello $this->name"; // it is declared by $name but access by only name.
    }
    }
    $obj = new Demo();
    $obj->name = 'mahtab';
    $obj1 = new Demo();
    $obj1->name='danish';
    $obj->sayhello();
    $obj1->sayhello();
    ?>
    Output : Hello mahtab
    Hello Danish

    File name : demo.php

    <?php
    class Demo
    {
    public $name;
    function sayhello()
    {
    print "Hello $this->name";
    }
    }

    ?>

    File name : test.php

    <?php
    Require_once('class.demo.php');
    $obj = new Demo();
    $obj->name = 'mahtab';

    $obj1 = new Demo();
    $obj1->name='danish';

    $obj->sayhello();
    $obj1->sayhello();
    ?>

    NOTE :-

  • the variable $this, that the object can get information about itself.
  • The $this variable enables you to refer to the current instance.




  • Previous Next


    Trending Tutorials




    Review & Rating

    0.0 / 5

    0 Review

    5
    (0)

    4
    (0)

    3
    (0)

    2
    (0)

    1
    (0)

    Write Review Here