OOP:如何使用方法之外的成员 [英] OOP: How to use a member outside a method

查看:75
本文介绍了OOP:如何使用方法之外的成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。我曾经有头发,但现在我不是。我们可以为此感谢OOP。 :)


我有一个员工班和第二个脚本,我的所有电话都是员工班的。


我需要建立对此应用程序的一些验证。我的问题是下拉框。


在我的第二个脚本中,我实例化了对象,一切都很好。我从方法中获取返回值,如果为true,则写入db,如果为false,则需要显示错误。这就是我所有问题的所在。


我想过使用SESSION却无法解决问题。我尝试了一个POST var,也无法让它工作。顺便说一句,这个表格回归自己。不知道为什么POST不起作用。


然后我突然意识到,我能想到的最简单的方法是创建另一个名为ShowError()的方法/>

[PHP]

公共函数ShowError($ error)

{

$ this->错误= $ error

返回$ this->错误;

}

[/ PHP]


在构造函数中,我已将var $ error初始化为成员并将变量设置为public。


在ShowError()方法之外,我试图调用会员,但不会回应。是什么赋予了?我知道我做错了什么但是我没有线索。


我应该使用全局吗?我以为OOP我可以在课堂上随叫随到那个成员。


有人可以帮忙吗?


谢谢,


Frank

Hello all. I used to have hair, but now I don''t. We can thank OOP for this. :)

I have an employee class and a second script where all of my calls are for the employee class.

I need to build some validation into this application. My problems are with dropdown boxes.

In my second script, I instantiate the object and all is well and good. I get a return value from the method and if true, write to the db, if false, I need to display an error. This is where all of my issues are at.

I thought about using a SESSION but couldn''t get that to work. I tried a POST var and couldn''t get that to work either. By the way, this form is posting back to itself. Not sure why POST wouldn''t work.

Then it dawned on me that the simplest way I can think of to accomplish this task is by creating another method called ShowError()

[PHP]
public function ShowError($error)
{
$this->error = $error
return $this->error;
}
[/PHP]

In the constructor, I have initialized the var $error as a member and set the variable as public.

Outside of the ShowError() method, I am attempting to call the member but it will not echo. What gives? I know I am doing something wrong but I haven''t got a clue.

Should I be using global for this? I thought with OOP I could call that member ANYWHERE in the class.

Could someone please help?

Thanks,

Frank

推荐答案

错误)

{
error)
{


this-> error =
this->error =


错误

return
error
return


这篇关于OOP:如何使用方法之外的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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