类和变量clairification [英] Classes and variable clairification

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

问题描述

您好,


有人可以帮帮我吗?我知道在类脚本中使用变量是一种很好的编程习惯,但它们也不是必需的。我想以正确的方式做事,所以我想我会问。


有一段时间我在脚本顶部声明了我的所有变量,如:

[PHP]

var $ myvar;

var $ mysecondvar;

...

[/ PHP]

我会离开()在我的所有方法中都是空白的,只需在我实例化对象的脚本中分配var,如下所示:

[PHP]

$ something = new something() ;

$ something-> myvar =''dsfsfjlk'';

$ something-> mysecondvar =''dafkad'';

[/ PHP]


查看完成的脚本后,我正在查看引用这些公共变量的很多行,现在想知道为什么我不能宣布括号内的变量而不是如上所述声明它们?


这里的区别是什么?我的意思是,如果我有一个变量在类中是全局的,我可以看到在括号外面声明变量而不是那个,在我看来,我应该将变量放在括号内。我是否正确?


我错过了哪个规则吗?


谢谢,


Frank

Hello,

Can someone please help me out? I know that using variables in a class script is a good programming practice but that they are also not required. I want to do things the correct way so I figured I would ask.

For a while I was declaring all of my vars at the top of the script like:
[PHP]
var $myvar;
var $mysecondvar;
...
[/PHP]
I would leave the () blank in ALL of my methods and just assign the var in the script where I instantiated the object like so:
[PHP]
$something = new something();
$something->myvar = ''dsfsfjlk'';
$something->mysecondvar = ''dafkad'';
[/PHP]

After looking at a completed script, I am looking at a lot of lines that reference those public vars and now want to know why couldn''t I just declare the vars within the parenthesis and NOT declare them as above?

What is the difference here? I mean, if I have a variable that will be global inside the class, I can see declaring the vars outside of the parenthesis but short of that, it seems to me that I should be placing the vars within the parenthesis. Am I correct?

Is there a rule about this somewhere I missed?

Thanks,

Frank

推荐答案

myvar;

var
myvar;
var


mysecondvar;

...

[/ PHP]

我会在我的所有方法中留下()空白,只是在我实例化对象的脚本中分配var像这样:

[PHP]
mysecondvar;
...
[/PHP]
I would leave the () blank in ALL of my methods and just assign the var in the script where I instantiated the object like so:
[PHP]


something = new something();
something = new something();


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

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