概述
* Used for checking empty objects/array
* @uses How to check empty objects and array in php code
* @author Aditya Mehrotra
*/
/**
* Empty class
*/classEmptyClass{
}$obj= newstdClass();//or any other class empty object$emptyClassObj= newEmptyClass();$array= array();
if (empty($array)) {
echo'array is empty';//expected result} else {
echo'array is not empty';//unexpected result}
echo"
";
if (empty($obj)) {
echo'object is empty';//expected result} else {
echo'object is not empty';//unexpected result}
echo"
";
if (empty($emptyClassObj)) {
echo'EmptyClass is empty';//expected result} else {
echo'EmptyClass is not empty';//unexpected result}
echo"
";//Result SET 1
//array is empty => expected result
//object is not empty => ouch what happened
//EmptyClass is not empty => ouch what happened
/**
* So what we do for checking empty object
* @solution use any known property or check property count
* Or you can use below method
* Count function will not return 0 in empty object
*/
//Below line print "Object count:1"echo'Object count:'.count($obj);
echo"
";/**
* This function is used to get object item counts
* @function getCount
* @access public
* @param object|array $var
* @return integer
*/functiongetCount($var) {$count=0;
if (is_array($var) ||is_object($var)) {
foreach ($varas$value) {$count++;
}
}
unset($value);
return$count;
}//Running code again with new logicif (getCount($array) ===0) {
echo'array is empty';//expected result} else {
echo'array is not empty';//unexpected result}
echo"
";
if (getCount($obj) ===0) {
echo'object is empty';//expected result} else {
echo'object is not empty';//unexpected result}
echo"
";
if (getCount($emptyClassObj) ===0) {
echo'EmptyClass is empty';//expected result} else {
echo'EmptyClass is not empty';//unexpected result}//Result SET 2
//array is empty => expected result ##everything is all right
//object is empty => expected result ##everything is all right
//EmptyClass is empty => expected result ##everything is all right
最后
以上就是欢呼大白为你收集整理的php 获取对象的所有属性_PHP: Object 对象 - Manual的全部内容,希望文章能够帮你解决php 获取对象的所有属性_PHP: Object 对象 - Manual所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复