static – PHP Blog https://blog.dev-php.site Snippets and guides Sun, 14 Apr 2019 06:48:57 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.2 Convert a PHP object to an associative array https://blog.dev-php.site/convert-a-php-object-to-an-associative-array/ Fri, 12 Apr 2019 01:48:16 +0000 http://blog.dev-php.site/?p=110 getProperties() as $property) { $property->setAccessible(true); … Continue reading Convert a PHP object to an associative array]]> // start class MyClass { public $var1 = 1; public $var2 = 2; public static $var3 = 3; protected $var4 = 4; private $var5 = 5; public function toArray() { return (array) get_object_vars($this); } } function toArray($object) { //$reflectionClass = new ReflectionClass(get_class($object)); $reflectionClass = new ReflectionObject($object); $array = []; foreach ($reflectionClass->getProperties() as $property) { $property->setAccessible(true); $array[$property->getName()] = $property->getValue($object); $property->setAccessible(false); } return $array; } $object = new MyClass(); $array = toArray($object); $array2 = $object->toArray(); print_r($array2); // Inaccessible NO static print_r($array); // Inaccessible and static $obArray = []; foreach($object as $ob){ $obArray[] = $ob; } print_r($obArray); // public only Array ( [var1] => 1 [var2] => 2 [var4] => 4 [var5] => 5 ) Array ( [var1] => 1 [var2] => 2 [var3] => 3 [var4] => 4 [var5] => 5 ) Array ( [0] => 1 [1] => 2 ) ]]> PHP: self vs $this https://blog.dev-php.site/php-self-vs-this/ Wed, 03 Apr 2019 19:38:21 +0000 http://blog.dev-php.site/?p=72 Continue reading PHP: self vs $this]]> Self:
-self refers to the current class
-self can be used to call static functions and reference static member variables
-self can be used inside static functions
-self can also turn off polymorphic behavior by bypassing the vtable

$this:
-$this refers to the current object
-$this can be used to call object functions
-$this should not be used to call static member variables. Use self instead.
-$this can not be used inside static functions

self = Current class.
static = Current class in runtime.
parent = Parent class.

class Test
{
    private $baz = 1;

	public static function foo() 
    { 
        echo "foo\n"; 
    }

  	public function bar()
  	{
    	echo "bar\n";
	    $this->foo();
    	$this::foo();
	    self::foo();
        self::baz();
  	}

	public function baz()
    {
        printf("baz = %d\n", $this->baz);
    }
}

$test = new Test;
$test->bar(); // bar foo foo foo baz = 1 


PHP manual
Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static can not be accessed with an instantiated class object (though a static method can).

Test::foo(); // foo

Is the same as

$test->foo(); // foo
]]>
Track number of objects created from a class https://blog.dev-php.site/track-number-of-objects-created-from-a-class/ Mon, 12 Feb 2018 04:36:50 +0000 http://blog.dev-php.site/?p=11 Continue reading Track number of objects created from a class]]> To track the existing number of objects of a given class create a static property to hold the current value, updated with the constructor. When unsetting an object, the __destruct() method will update the object count. You can create new instances without hitting the constructor via clone and deleted with unset()


/*
* Class TrackableClass 
*/
class TrackableClass {
    public static $instances = 0;

    public function __construct() 
    {
        self::$instances++;
    }

    public function __destruct() 
    {
        self::$instances--;
    }
	
    public function __clone() 
    {
	self::$instances++;
    }
	
    public function __unset($name)
    {
        self::$instances--;
    }
	
}

$obj1 = new TrackableClass();
$obj2 = new TrackableClass();
$obj3 = new TrackableClass();
$obj4 = clone $obj3;

echo TrackableClass::$instances; // 4
unset($obj4);
echo TrackableClass::$instances; // 3
$obj3 = null;
echo TrackableClass::$instances; // 2
]]>
PHP Method Chaining https://blog.dev-php.site/php-method-chaining/ Thu, 08 Feb 2018 02:22:17 +0000 http://blog.dev-php.site/?p=4 Continue reading PHP Method Chaining]]> Method chaining, also known as named parameter idiom, is a common syntax for invoking multiple method calls in object-oriented programming languages. Each method returns an object, allowing the calls to be chained together in a single statement without requiring variables to store the intermediate results


/*
* ChainableClass
* cascading-by-chaining by returning this
*/
class ChainableClass {
  function methodOne() {
    echo __METHOD__.' called, ';
    return $this;
  }
  function methodTwo() {
    echo __METHOD__.' called';
    return $this;
  }
}
(new ChainableClass)->methodOne()->methodTwo();
// Result: BaseClass::methodOne called, BaseClass::methodTwo called

With Static


/*
* ChainableClass
* cascading-by-chaining by returning self
*/
class ChainableClassStatic {
  public static $prop;

  public static function ChainableStaticMethod()
  {
    self::$prop = 'Chainable Class String';
    return new self;
  }

  public function regularMethod()
  {
    echo self::$prop;
  }
}
ChainableClassStatic::ChainableStaticMethod()->regularMethod();
// Result: Chainable Class String
]]>