Search
 
SCRIPT & CODE EXAMPLE
 

PHP

static php

<?php
  //Explanation and features
  //1- we can call the static methode  and  property without creating instance
  //from the class  => syntax to call is : classname::methodename(),ore propName.
  // static property save the new value in the function . 
  
  class carA {
  static private $color="red";
  public static function size (){return "big"}; 
  
  
} 

class carB { $color private $color =" black" ;
              public  function size (){return "small"}; 
            //to access a static property to a public function in the same class =>
              static private $price= 3000; 
           public function price(){ 
            return self::$price;
              
            }
            


           }

$carOb = new carB(); 
echo $carOb->color;//output=>black;
echo $carOb->size();//output=>small;
echo $carOb->price();//output=>3000;



//to access to the static( property and methodes) className::propName/methodeName
echo carA::$color; //output is : red;
  echo carA::size(); //output is : big;

  
  
  
  
  
  
  
Comment

php static variable

/*
  1) Example demonstrating need for static variables
  This function is quite useless since every time it is called 
  it sets $a to 0 and prints 0. 
  The $a++ which increments the variable serves no purpose 
  since as soon as the function exits the $a variable disappears.
  To make a useful counting function which will not lose track of the current count, 
  the $a variable is declared static:
*/
function test()
{
    $a = 0;
    echo $a;
    $a++;
}

/*
  2) Example use of static variables
  A static variable exists only in a local function scope, 
  but it does not lose its value when program execution leaves this scope.
*/
function newTest()
{
    static $a = 0;
    echo $a;
    $a++;
}
Comment

static function php

<?php
class Car
{
    public function test($var = 'Hello kinjal')
    {
        $this->var = $var;
        return $this->var;
    }
}
class Bike
{
    public static function test($var)
    {
        $var = '<br>this is static function';
        return $var;
    }
}
$obj = new Car();
echo $obj->test();
echo Bike::test('this is non static');      //static function called using :: double colon

?>
Comment

static variable php


<?php
function foo(){
    static $int = 0;          // correct 
    static $int = 1+2;        // correct
    static $int = sqrt(121);  // wrong  (as it is a function)

    $int++;
    echo $int;
}
?>

Comment

static functions php

class Products {

	protected $id;
    
    public function setId($id)
    { 
    	return $this->id = $id;
    }
    
    public function getProduct()
    { 
    	$arr_list = [
        				[
                          'name'=>'Proton',
                          'year' => 1987
                        ],
                        [
                          'name'=>'Produa',
                          'year' => 1990
                        ],
                    ];
    	return $arr_list;
    }

	public static function product_list()
    {
    	//whatever data want to return
        $arr_list = [
        				[
                          'name'=>'Proton',
                          'year' => 1987
                        ],
                        [
                          'name'=>'Produa',
                          'year' => 1990
                        ],
                    ];
    	return $arr_list;
    }
    
    
}

//static can call directly  
$lists = Product::product_list();

//non static
$product = new Product();
$lists = $product->getProduct();

//when calling class must alert memory consume. 
Comment

PREVIOUS NEXT
Code Example
Php :: db transaction laravel 
Php :: laravel check if email is real 
Php :: laravel collection max 
Php :: php italian date 
Php :: php check valid json string 
Php :: change the method name in resource in laravel 
Php :: laravel migrate test environment 
Php :: php currency formator 
Php :: POP UP WITH PHP 
Php :: update image in database using php 
Php :: php associative array join key values 
Php :: php get first day of month 
Php :: laravel Please provide a valid cache path 
Php :: mysql get number of rows php 
Php :: img upload in php 
Php :: php invoke 
Php :: laravel switch 
Php :: laravel mysql specified key was too long 
Php :: random string value laravel 
Php :: i+= in php 
Php :: unset php return array 
Php :: update laravel 
Php :: contact form 7 get form id 
Php :: How to create a route in laravel? 
Php :: laravel 8 add column to existing table 
Php :: laravel Auth::logoutOtherDevices 
Php :: integrate fontawesome in blade laravel 
Php :: symfony messenger 
Php :: get the number of affected rows in php using pdo update statement 
Php :: fgets in php 
ADD CONTENT
Topic
Content
Source link
Name
7+7 =