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

PREVIOUS NEXT
Code Example
Php :: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given 
Php :: laravel getClientOriginalExtension 
Php :: laravel password test 
Php :: register_uninstall_hook 
Php :: php version not update after windows env file 
Php :: laravel validation two columns unique 
Php :: php user ip from post request 
Php :: Invalid datetime format: 1366 Incorrect string value 
Php :: sql query show table phpmyadmin 
Php :: laravel hide columns 
Php :: magento 2 remove order 
Php :: toast in laravel 
Php :: convertidos de 24 12 hr php 
Php :: laravel override eloquent all function 
Php :: how does substr_compare() works PHP 
Php :: Route pattern cannot reference variable name more than once. laravel 
Php :: laravel translation parameter send 
Php :: php.validate.executablepath docker 
Php :: create migration command in laravel 
Php :: get object value by key php 
Php :: save big data with laravel 
Php :: * * * * * cd /path-to-your-project && php artisan schedule:run /dev/null 2&1 
Php :: causes of class not found in laravel 
Php :: twig render to string 
Php :: laravel pluck multiple columns 
Php :: laravel get route action 
Php :: what are the different types of PHP variables? 
Php :: add javascript to wordpress functions php 
Php :: SIMPLE linked list in php 
Php :: php in html need .htaccess 
ADD CONTENT
Topic
Content
Source link
Name
7+3 =