没有实例的类 [英] Classes without instances

查看:116
本文介绍了没有实例的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




在很多情况下,脚本中只需要一个类的单个实例。

因为PHP5提供了像MyClass这样的类变量: :$ myVar甚至类

常量,在我看来不需要实例化。是的,因为没有实例化,使用一个类是好的和良好做法吗?在

这种情况​​下,我可以将这样的类视为函数集合和

变量,它们在某种程度上属于一起,并且前缀类名称可以是

作为一种命名空间标识符:


包含myLib.php

myLib :: $ somevar =''some value'';

myLib :: somefunc(myLib :: MY_CONSTANT);


另一个问题:在PHP4中我可以有


class myLib {function myFunc(){/ * do stuff * /}}

myLib :: myFunc();


在PHP5中,有"静"关键词。究竟有什么区别



class myLib {public static function myFunc(){/ * do stuff * /}}

class myLib {public function myFunc(){/ * do stuff * /}}

问候,

Thomas

解决方案

myVar甚至类

常量,在我看来不需要实例化。是的,因为没有实例化,使用一个类是好的和良好做法吗?在

这种情况​​下,我可以将这样的类视为函数集合和

变量,它们在某种程度上属于一起,并且前缀类名称可以是

作为一种命名空间标识符:


包含myLib.php

myLib ::


somevar =''some value'';

myLib :: somefunc(myLib :: MY_CONSTANT);


另一个问题:在PHP4中我可以拥有


class myLib {function myFunc(){/ * do stuff * /}}

myLib :: myFunc();


在PHP5中,有静态和静态。关键词。究竟有什么区别



class myLib {public static function myFunc(){/ * do stuff * /}}

class myLib {public function myFunc(){/ * do stuff * /}}

问候,

Thomas


>在PHP5中,存在静态和静态。关键词。

类myLib {public static function myFunc(){/ * do stuff * /}}
class myLib {public function myFunc - 究竟有什么区别

(){/ *做东西* /}}




在第一种情况下,函数myFunc()将在没有

实例化的情况下可用。

然后你可以打电话给

myLib :: myFunc()

第二种情况你不能


在PHP 4中没关系,你可以随时(?)调用一个函数

静态

这可能导致奇怪的结果(可能使用的函数)一个变量

尚未初始化。

现在在PHP 5中我们有静态


Mei?o


Hi,

In many cases, one needs only a single instance of a class in a script.
Since PHP5 offers class variables like MyClass::$myVar and even class
constants, it seems to me that an instantiation is not needed. Is it
therefore okay and good practise to use a class without instantiation? In
that case I could regard such a class as a collection of functions and
variables that belong together in a way and the prefixed class name could
serve as a kind of namespace identifier:

include "myLib.php"
myLib::$somevar = ''some value'';
myLib::somefunc( myLib::MY_CONSTANT );

Another question: In PHP4 I can have

class myLib { function myFunc() { /* do stuff */ } }
myLib::myFunc();

In PHP5, there is the "static" keyword. What exactly is the difference
between

class myLib { public static function myFunc() { /* do stuff */ } }
class myLib { public function myFunc() { /* do stuff */ } }

Greetings,
Thomas

解决方案

myVar and even class
constants, it seems to me that an instantiation is not needed. Is it
therefore okay and good practise to use a class without instantiation? In
that case I could regard such a class as a collection of functions and
variables that belong together in a way and the prefixed class name could
serve as a kind of namespace identifier:

include "myLib.php"
myLib::


somevar = ''some value'';
myLib::somefunc( myLib::MY_CONSTANT );

Another question: In PHP4 I can have

class myLib { function myFunc() { /* do stuff */ } }
myLib::myFunc();

In PHP5, there is the "static" keyword. What exactly is the difference
between

class myLib { public static function myFunc() { /* do stuff */ } }
class myLib { public function myFunc() { /* do stuff */ } }

Greetings,
Thomas


> In PHP5, there is the "static" keyword. What exactly is the difference

between

class myLib { public static function myFunc() { /* do stuff */ } }
class myLib { public function myFunc() { /* do stuff */ } }



in this first case the function myFunc() will be available without
instantiation.
then you can call
myLib::myFunc()
second case you can''t

In PHP 4 it didn''t matter, you could always (?) call a function
staticly
which may lead to strange results (maybe the function used a variable
which hasn''t been initialized).
Now in PHP 5 we have static

Mei?o


这篇关于没有实例的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆