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

Calling itself a static function in php

//https://www.php.net/manual/en/language.oop5.static.php

self::staticMethod();
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 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 :: php domdocument get elements one by one 
Php :: Header requirements for new plugin in wordpress 
Php :: wc get_image() return image url 
Php :: menu order for posts 
Php :: $n = readline(); for($i = 0; $i < $n ; $i++) { $name = readline(); $names[$name] = (isset($names[$name]) ? $names[$name] + 1 : 1); } 
Php :: markdown mail html rendering laravel 
Php :: wordpress remove current post in sidebar php 
Php :: Nginx + Laravel - Moving blog from subdomain to /blog 
Php :: Uncaught TypeError: call_user_func(): Argument #1 
Php :: js data php 
Php :: laravel 8 template favicon 
Php :: laravel read csv 
Php :: laravel after method() 
Php :: Error when uploading image into phpmyadmin using PDO in php 
Php :: the_field 
Php :: php check if cli mode 
Php :: selecting values from database 
Php :: create newfilter wordpress 
Php :: protocals supported by php 
Php :: auto complete order paid 
Php :: load more data on button click in laravel 
Php :: withCount laravel assign generic name 
Php :: php vender 403 forbidden 
Php :: laravel openstreetmap 
Php :: br2nl 
Php :: php check if variable is true or false 
Php :: $this meaning in codeigniter 
Php :: php discord webhook sender 
Php :: how to refresh a php variable without reloading page 
Php :: what does php stand for 
ADD CONTENT
Topic
Content
Source link
Name
5+3 =