new 和 static
有几个页面要用到的语句是一样的,为了方便改动,当然是放在一个通用的类里。
第一种:
int Num = CommonRequest.GetUserLoginTimes(user)代码:
public class CommonRequest//通用
{
public static int GetUserLoginTimes(User user)//static
{
return User.Type == "admin"?ServiceFactory.GetFactory().GetUserService().GetUserLoginTimes(User.userId):0;
}
}
第二种:
UserRequest ur = new UserRequest();//new
int Num = ur.GetUserLoginTimes(user)
代码:
public class UserRequest//独立类
{
public int GetUserLoginTimes(User user)
{
return User.Type == "admin"?ServiceFactory.GetFactory().GetUserService().GetUserLoginTimes(User.userId):0;
}
}
请问这有什么区别吗?
相关推荐
PHP中new static() 和 new self() 的区别介绍
主要介绍了PHP中new static() 和 new self() 的区别介绍,需要的朋友可以参考下
PHP中new static和new self的区别介绍
当我们在ChildClass中调用create()方法时,它返回的是ChildClass的实例,而不是ParentClass的实例。这意味着,如果我们使用继承来创建一个子类,并且在子类中调用了new static,它将返回子类的实例,而不是父类的实的实例。无论我们在哪个类中调用create()方法,它都只会返回ParentClass的实例,因为new self只会返回当前调用类的实例。当我们使用new self关键字创建对象实例时,它会始终返回当前调用的类的实例,而不考虑是否存在继承关系。
PHP的new static和new self的区别与使用
下面我们举个栗子: class Father { public static function getSelf() { return new self(); } public static function getStatic() { return new static(); } } class Son extends Father {} echo get_class(Son::getSelf()); // Father echo get_class(Son::getStatic()); // Son echo get_class(Father::getSel
浅谈PHP中new self()和new static()的区别
本文介绍了PHP中new self()和new static()的区别,分享给大家,也给自己留个笔记。 1.new static()是在PHP5.3版本中引入的新特性。 2.无论是new static()还是new self(),都是new了一个新的对象。 3.这两个方法new出来的对象有什么区别呢,说白了就是new出来的到底是同一个类实例还是不同的类实例呢? 为了探究上面的问题,我们先上一段简单的代码: class Father { public function getNewFather() { return new self(); } public function get
PHP new static 和 new self详解
使用 self:: 或者 __CLASS__ 对当前类的静态引用,取决于定义当前方法所在的类:使用 static:: 不再被解析为定义当前方法所在的类,而是在实际运行时计算的。也可以称之为“静态绑定”,因为它可以用于(但不限于)静态方法的调用。



