📜 ⬆️ ⬇️

Functions for working with objects work with classes (PHP 5.2)

It turns out that the specification of some functions for working with classes is not accurate enough.

ru2.php.net/manual/ru/function.is-subclass-of.php
is_subclass_of - Returns TRUE if this is one of its parents
bool is_subclass_of (object object, string class_name);
This function works fine with the class name.

for example, you can view all child classes:
 function getClassChildren ($ parentClassName) {
	 $ result = array ();
	 foreach (get_declared_classes () as $ className) {
		 if (is_subclass_of (strtolower ($ className), $ parentClassName) or $ className == $ parentClassName)
			 $ result [] = $ className;
	 }
	 return $ result;
 }

Although I do not advise you to do this, but I heard that some wanted to see an example of this hack, I hope you will find a way to do without it ;-)
')
get_parent_class - Returns the base class name for an object or class.
string get_parent_class (mixed $ obj)

Here it is honestly said that the argument is of a mixed type, but many (judging by my friends) do not pay attention to this. This function also works wonderfully with the class name.

surprised by the function method_exists ();
 <?
 class A {
	 private $ a1 = "test";
	 public $ a2 = "tt";
	 static public function aa () {}
 }
 class B extends A {
	 static public function bb () {}
 }
 $ a = new A;
 var_dump (method_exists ("A", 'aa'));
 $ b = new B;
 var_dump (method_exists ("B", 'aa'));
 ?>


answer :
bool (true)
bool (true)

that is, it really looks to see if there is a method for the objects being created, and not for the class.
Of course, the function contradicts the ideology of interfaces, but sometimes it is very convenient to use it.

call_user_method does not tolerate distortions:
 <?
 class A {
	 static public function aa () {
		 echo 'aaa';
	 }
 }
 call_user_method ('aa', 'A');
 ?>
Fatal error will be your answer: Only variables can be passed by reference
I ask to perceive the topic as a reminder, maybe a discussion

Source: https://habr.com/ru/post/44298/


All Articles