https://yourshala.com YourShala - Easy way to learn programming Sun, 08 Dec 2024 08:29:11 +0000 en-US hourly 1 https://wordpress.org/?v=6.7.1 Static properties in PHP https://yourshala.com/php/static-properties-in-php/ Sun, 08 Dec 2024 08:28:30 +0000 https://yourshala.com/?p=1523 Static properties in PHP, can be used directly without creating class object. It can be used with class name only.

Syntax

static variablename;

Creating Static property

static keyword is used before property/variable name.

<?php
class myClass{
   
 public static $number = 20;

}
?>

Accessing static property

Scope resolution operator :: is used to access static property with class name.

<?php
class myClass{
   
  public static $number = 20;

}
// Accessing static property with classname and scope resolution operator
echo myClass::$number;
?>
]]>
1523
Static methods PHP https://yourshala.com/php/static-methods-php/ Sun, 08 Dec 2024 08:24:46 +0000 https://yourshala.com/?p=1515 Static methods are the functions of a class, which are used directly without creating class object.

Syntax

class classname{
static function functionname(arguments){
// set of statements
 }
}

static functions/methods are defined using static keyword then function keyword and functionname.

Example

class myClass{
static function myStaticFunction(){
	echo "Hello";
 }
}

Accessing static method

To access or use static method of a class, classname with scope resolution operator i.e. :: and static method name is used. No object is needed to access static method.

Example

class myClass{
static function myStaticFunction(){
	echo "Hello";
 }
}
// Accessing static method with classname and scope resolution operator
myclass::myStaticFunction();

]]>
1515
Traits in PHP https://yourshala.com/php/traits-in-php/ Sun, 08 Dec 2024 05:48:44 +0000 https://yourshala.com/?p=1510 Traits in PHP, allows to use or share its code in multiple classes without inheritance. Following are the features of using traits in PHP.

  1. Trait is introduced in PHP version 5.4
  2. It allows to reuse code in multiple classes.
  3. Traits objects cannot be created. it can only be used by classes.
  4. Trait removes the problem of single inheritance, by getting used in different classes despite of hierarchy.
  5. Class can use more than one traits.
  6. A trait can use another trait.

Syntax

trait traitname{

 public function function_name(arguments){
    //set of statements
 }

}

Create a Trait

trait myTrait{

 public function sayHello(){
    return "Hello";
 }

}

Using a Trait

To use a trait in a class, use keyword is used with trait name.

<?php
trait myTrait{

 public function sayHello(){
    return "Hello";
 }

}

class myClass{
use myTrait;
  
}

$obj = new myClass();
echo $obj->sayHello();

?>

Using multiple Traits in a class

To use multiple traits in a class, use keyword is followed by traits name are separated by comma.

Syntax

<?php
class myclass{

 use trait1, trait2, trait3;

}
?>

Example

<?php

trait myTrait{

 public function sayHello(){
    return "Hello";
 }

}

trait Byee{

 public function sayBye(){
    return '<br/>'. "Bye";
 }

}


class myClass{
use myTrait, Byee;
  
}

$obj = new myClass();
echo $obj->sayHello();
echo $obj->sayBye();

?>
]]>
1510
Namespaces in PHP https://yourshala.com/php/namespaces-in-php/ Sat, 07 Dec 2024 13:27:15 +0000 https://yourshala.com/?p=1492 Namespaces in PHP, are used to group classes, interfaces etc under a given name, so that same names does not conflict with each other. By keeping same names under different namespaces, name collision can be avoided.

File with Same name cannot exists in same folder or directory, Similarly in PHP classes, interfaces etc with same name cannot exists in same PHP program.

Without namespaces, Using same name for class, interface, traits, abstract class, function or constants gives error in PHP program, because of name confliction or collision. To overcome this name confliction problem, PHP provides namespaces, so same name can be used or kept under different namespaces.

  1. Namespace can contain class, interface, traits, abstract class, function and constants.
  2. It provides logical grouping or act as container.
  3. Same name should not be used in namespace.
  4. Same names must be used in different namespaces to avoid naming collision.
  5. It avoids name confliction between your code and PHP built-in or third-party classes/trait/interfaces/functions etc.

Syntax

<?php
namespace namespace_name;

// PHP Code
?>

Creating Namespace

Namespace must be written at the top of the code. To create a namespace, namespace keyword is used, followed by name and semicolon.

<?php
namespace myName;

// PHP Code
?>

Multiple Namespaces in same file

PHP allows to create multiple namespaces in a single PHP script.

<?php
namespace myName1;

// PHP Code

namespace myName2;

// PHP Code


namespace myName3;

// PHP Code
?>

Namespace Name collision

To avoid name collision same class name is used and kept under different namespaces.

<?php
namespace myName1;

class Math{

 }

namespace myName2;

class Math{

 }

?>

Namespace Alias

Alias is use to provide a short or easier name to namespaces or a class. PHP provides use keyword to create an alias.

File name: MobilePhone.php

<?php
namespace MobilePhone;
echo "Hello";
?>

MobilePhone.php file namespace MobilePhone is used in index.php file.

  1. First include a file which has namespaces.
  2. To use namespaces of included file, PHP use and as keyword is used.

File name: index.php

<?php
include "MobilePhone.php";
use MobilePhone as Samsung;
?>

Sub namespaces

Sub namespaces allows to create a namespace inside a namespace, it is like hierarchy or sub-levels.
sub namespaces are separated by backslash.

Syntax

namespace namespace\subnamespace\subnamespace

Example

<?php
 namespace namespace\namespace\namespace3
?>
]]>
1492
Inheritance in PHP https://yourshala.com/php/inheritance-in-php/ Sat, 07 Dec 2024 13:14:07 +0000 https://yourshala.com/?p=1486 Inheritance in PHP, is inheriting a class with another class, a class which is inherited called parent class and which inherits is called child class. In inheritance, child class uses (inherits) all parents class, public and protected data members and member functions. Private members cannot be inherited.

Also Called
Parent classBase, Super class
Child classDerived, Subclass

Advantages of Inheritance

Inheritance allows to reuse code of a class in another class.

Example

<?php
class MyParent{
 public $parentname;

 function setParentName($n){
      $this->parentname = $n;
 }

}

class child1 extends MyParent{
 public $name;

 function setChildName($n){
      $this->name = $n;
 }

}

$obj =  new child1();
//Calling Parent class function with child class object
$obj->setParentName('PHP');
//Calling child class function with child class object itself
$obj->setChildName('Inheritance');
?>
]]>
1486
Interface PHP https://yourshala.com/php/interface-php/ Sat, 07 Dec 2024 13:08:36 +0000 https://yourshala.com/?p=1476 In PHP, Interface acts as an agreement with implementing class, it must implement all its methods. Following are the features of PHP interfaces.

  1. All Interfaces methods are public.
  2. A class can implement more than one interface.
  3. Interface cannot have properties.
  4. To implement an interface, implement keyword is used.
  5. An interface, objects cannot be created means it cannot be instantiated.
  6. Interface allows multiple inheritance.

Interface Usage

When so many classes have to implement same method for different purpose, interface should be used. So, by this way code reusability and polymorphism is achieved in PHP with interfaces.

Syntax

interface myInterface{
 public function myFunction();
 public function myFunction1(argument,argument1);
}

Interface is declared using interface keyword and interface name with curly braces {}, inside these curly braces interface function must be declared.

Create an interface PHP

interface Language{
 public function language_name($name);
}

Example

<?php
interface Language{
 public function language_name($name);
}

class Programming implements Language{

 public function language_name($name){
   echo $name;
 }
}

class OfficialLanguage implements Language{

 public function language_name($name){
   echo $name;
 }

}

$obj = new Programming();
$obj->language_name('PHP');

$obj = new OfficialLanguage();
$obj->language_name('English');

?>
  1. interface Language with function language_name is declared with a function signature language_name();
  2. class OfficialLanguage and Programming implemented Language interface.
  3. Due to this, class OfficialLanguage and Programming has to implement or provide declaration to all interface Language functions.

Multiple interface with single class

A class can implement one or more than one interfaces, Below is an example.

<?php
interface Country{
 public function country_name($name);
}

interface Language{
 public function language_name($name);
}

class People implements Country, Language{

 public function language_name($name){
   echo $name;
 }
 public function country_name($name){
   echo $name;
 }

}

$obj = new People();
$obj->country_name('USA');
$obj = new People();
$obj->language_name('English');
?>
]]>
1476
Abstract class in PHP https://yourshala.com/php/abstract-class-in-php/ Sat, 07 Dec 2024 09:21:12 +0000 https://yourshala.com/?p=1468 In PHP, Abstract class cannot be instantiated (means object cannot be created), to use abstract class, other class must extend it and implement all its abstract methods. Abstract class can have both abstract or non abstract (concrete) methods. Abstract methods only provide method signature without function body {} and its code.

Because object of Abstract classes cannot be created. Abstract classes act as template for other classes to extend.

  1. Abstract class must have at least one abstract method.
  2. Constructor can also be created inside abstract class in PHP
  3. A class can only extend one abstract class.
  4. It provides common Abstract methods for other classes to implement.

Syntax

An Abstract class declaration is similar to other classes except abstract keyword is used before class keyword.

abstract class classname{

// abstract method
abstract function function_name();
abstract function function_name(arguments);


 // Non-abstract method
function function_name(){
 // set of statements
}
function function_name(arguments){
 // set of statements
}

}

Abstract method are like normal methods, without implementation only means no function body and statements, it can also accept arguments.

Abstract class with abstract method only

<?php
   
 abstract class myAbstract{
// abstract method
  abstract function add($a,$b);

 }

class Math extends myAbstract{
 
   // Implementing abstract class method
  function add($a,$b){
      return ($a + $b);
  }

}

$obj = new Math();
echo $obj->add(20,30);
?>

In above example, class myAbstract is abstract class, With an abstract method named add() which accepts two arguments $a, $b.

  1. Class Math is a concrete or non-abstract class which extends myAbstract class.
  2. Therefore, Math has to implement all its abstract methods of myAbstract class.
  3. In Math, myAbstract class, abstract method add() is implemented and it returns the sum of $a and $b.

Abstract class with abstract and non-abstract methods

<?php
abstract class myAbstract{
  // abstract method
  abstract function add($a,$b);

  //Non-abstract method
  function sayHello(){
   echo "<br/>"."Hello";
  }
 }

class Math extends myAbstract{
 
  function add($a,$b){
      return ($a + $b);
  }

}

$obj = new Math();
echo $obj->add(20,30);
echo $obj->sayHello();
?>

Non-abstract method sayHello() is called using Math class object.

]]>
1468
Access modifiers in PHP https://yourshala.com/php/access-modifiers-in-php/ Sat, 07 Dec 2024 05:29:47 +0000 https://yourshala.com/?p=1460 Access modifiers in class PHP, controls the accessibility(use) of its data members and member function. On this basis, a class in PHP has three access modifiers.

  1. public
  2. private
  3. protected
    publicThese properties or methods can be used inside and outside of class without restrictions
    privateThese properties or methods can only be used within or by class.
    protectedThese properties or methods can be used by class or its derived/child class.

    public Access modifiers

    class Student{
     public $marks;
    }
    
    $obj =  new Student();
    $obj->marks = 60;
    echo $obj->$marks;
    class Student{
     public $marks;
    
     function setMarks($m){
          $this->marks = $m;
     }
    }
    
    $obj =  new Student();
    $obj->setMarks(70);

    Private Access modifiers

    class Student{
     private $marks;
    }
    
    $obj =  new Student();
     // Error, because private members are not accessible outside class
    $obj->marks = 60;
    echo $obj->$marks;
    class Student{
     private $marks;
    
     private function setMarks($mr){
          $this->marks = $mr;
     }
    }
    
    $obj =  new Student();
    $obj->setMarks(90); // Error. Private members cannot be accessed.

    Protected Access modifiers

    class Student{
     protected $marks;
    }
    
    $obj =  new Student();
    $obj->marks = 60; // Error, because protected members are not accessible outside class
    echo $obj->$marks;
    
    class Student{
     protected $marks;
    
     protected function setMarks($mr){
          $this->marks = $mr;
     }
    }
    
    $obj =  new Student();
    $obj->setMarks(90); // Error. protected members cannot be accessed.
    ]]>
    1460
    Destructor in PHP https://yourshala.com/php/1458/ Sat, 07 Dec 2024 05:22:27 +0000 https://yourshala.com/?p=1458 Destructor in PHP is used to deleted or remove object of class. It is automatically called by PHP when script is about to end.

    __destruct Function

    To add Destructor in class, PHP provides __destruct function, it does not accepts any arguments.

    Syntax

    function __destruct(){
    
    }

    Example

    <?php
    
    class Mobile{
     
    // Properties
      public $company;
    
      // constructor
    function __construct($company) {
        $this->company = $company;
      }
     
     // destructor 
    function __destruct($company) {
        $this->company = $company;
      }
    
    }
    
    Mobile myPhone = new Mobile("Motorola");
    ?>

    ]]>
    1458
    Constructor in PHP https://yourshala.com/php/constructor-in-php/ Sat, 07 Dec 2024 05:14:37 +0000 https://yourshala.com/?p=1456 Constructor in PHP, is a function which is called or invoked automatically when a class object is created. It is used to initialize data members while object creation.

    __construct Function

    To add constructor in class, PHP provides __construct function, it can accept any number of arguments.

    function keyword than Double underscore and construct keyword without space and the () function symbol is used to create constructor.

    Syntax

    function __construct($argument1, $argument2,...){
    
    }

    Example

    <?php
    
    class Mobile{
     
    // Properties
      public $company;
    
      // constructor
    function __construct($company) {
        $this->company = $company;
      }
      function get_name() {
        return $this->$company;
      }
    
    }
    
    Mobile myPhone = new Mobile("Motorola");
    myPhone->get_name();
    
    ?>
    ]]>
    1456