Answers for "static function php"

PHP
3

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

?>
Posted by: Guest on September-10-2021
2

class static php

<?php
class ClassName {
  public static function staticMethod() {
    echo "Hello World!";
  }
}
?>
Posted by: Guest on March-07-2021
0

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;
}
?>
Posted by: Guest on June-22-2021
0

php access non static function

<?php
	class Toys
	{
		private $toys_name;
		public $toys_category;
		public static $shop_name;

		function Toys($name,$category) 
		{
			$this->toys_name = $name;
			$this->toys_category = $category;
		}

		public function getToyName() 
		{ 
			return $this->toys_name; 
		}

		public function getToyCategory() 
		{ 
			return $this->toys_category; 
		}	

		public function getToyShop_nonStatic() 
		{ 
			return self::getToyShop(); 
		}

		public static function getToyShop() 
		{ 
			return self::$shop_name;
		}

		public static function setToyShop($shopname) 
		{ 
			self::$shop_name=$shopname; 
		}

	}

	$objToys = new Toys("Battery Car","Battery Toys");
	$toys_name = $objToys->getToyName();
	$toys_category = $objToys->getToyCategory();
	echo "<br/>Toy: " . $toys_name . ", Category: " . $toys_category;

	Toys::$shop_name = "Disney";
	$shop_name = Toys::getToyShop();
	echo "<br/>Shop Name: " . $shop_name;

	Toys::setToyShop("ToyShop");
	$shopname = Toys::getToyShop_nonStatic();
	echo "<br/>Shop Name via non static function: " . $shopname;

?>
Posted by: Guest on December-26-2020

Browse Popular Code Answers by Language