获取和设置以及最佳实践 [英] Getting and Setting and best practise

查看:54
本文介绍了获取和设置以及最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我有一个代表项目的对象在CMS组件中其中

" item"在最基本的形式中只是一个字段,而组件是一个字段。有效地是一张桌子。


" item"可以创建对象,然后将其添加到组件中。对象来构建组件定义。


我的困境在于决定如何读取/写入数据到item

对象。


我想我可以:


1.使用getter和setter(尽管讨厌他们的想法)

2.使用__get和__set我更喜欢用户的想法

接口的观点虽然我明白他们没有注意到

会员可见性。

3.将所有必需的参数传递给一个函数,并在那里进行所有

验证 - 不灵活,不直观且更长时间工作

在我看来运行。


我倾向于__get和__set路线所以我能做到:


$ item = new item(''user'');


//可以在执行验证的函数中抛出异常

$ item-> required = true ;

$ item-> label =''用户';

$ item-> description =''项目'';


//对项目的所有验证都已完成,所以只需添加它

$ component-> add_item($ item );

这有意义吗?我很想知道其他什么方法

人们会选择以及为什么。


感谢您的任何意见。


吉米。

Hi guys,

I have an object which represents an "item" in a CMS "component" where
an "item" in the most basic form just a field, and a "component" is
effectively a table.

"item" objects can be created and then added to "component" objects to
build up the component definition.

My dilemma comes in deciding how to read/write data to the "item"
object.

I figure I can either:

1. Use getters and setters (hate the idea of them though)
2. Use __get and __set which I prefer the idea of from a user
interface point of view though I understand they take no notice of
member visibility.
3. Pass all the required parameters to one function and do all the
validation there - inflexible, unintuitive and more work in the long
run in my opinion.

I''m leaning towards the __get and __set route so I could do:

$item = new Item(''user'');

// could throw exceptions in the functions that do validation
$item->required = true;
$item->label = ''User'';
$item->description = ''A description of the item'';

// all validation on the item is done so just add it
$component->add_item($item);
Does this make sense? I''m interested in hearing what other methods
people would choose and why.

Thanks for any input.

Jimmy.

推荐答案

item = new Item(''user'');


//可能会在执行验证的函数中抛出异常
item = new Item(''user'');

// could throw exceptions in the functions that do validation


item-> required = true;
item->required = true;


item-> label =''User'';
item->label = ''User'';


这篇关于获取和设置以及最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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