// 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 )
Tag: objects
Merging PHP Objects
Given two objects of same class, merge both objects into a single object.
class MyClass {} $objectA = new MyClass(); $objectA->a = 1; $objectA->b = 2; $objectA->d = 3; $objectB = new MyClass(); $objectB->d = 4; $objectB->e = 5; $objectB->f = 6; // Using array_merge $obj_merged = (object) array_merge((array) $objectA, (array) $objectB); print_r($obj_merged); stdClass Object ( [a] => 1 [b] => 2 [d] => 4 [e] => 5 [f] => 6 ) // Using array union operator $obj_union = (object)((array) $objectA +(array) $objectB); print_r($obj_union); stdClass Object ( [a] => 1 [b] => 2 [d] => 3 [e] => 5 [f] => 6 )
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
Counting a PHP stdClass Object
$obj = new stdClass; $obj->one = 1; $obj->two = 2; $vars = get_object_vars($obj); echo count($vars);
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
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