how to handle the states of an object [closed]
What would be the correct way to handle the types of state that an object can have in an application?
For example, if i have an AcceptanceCriteria class, i need to verify if it is accepted, rejected or pending.
I usually do it by returning numbers that represent the state, but it does not seem like a good form, it can be confusing.
for example:
class AcceptanceCriteria
{
const PENDING = 0;
const ACCEPTED = 1;
const REJECTED = 2;
protected $state = self::PENDING;
public function accept():void
{
$this->state = self::ACCEPTED;
}
public function reject():void
{
$this->state = self::REJECTED;
}
public function state():int
{
return $this->state;
}
}
I need to check the state frequently and also show it in the front, what is a better way to do it? I dont want to check in the front if and acceptance criteria state is 0, 1 or 2 for do something.
php oop
closed as primarily opinion-based by Paul, david, rickvdbosch, greg-449, Billal Begueradj Dec 31 '18 at 9:28
Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
What would be the correct way to handle the types of state that an object can have in an application?
For example, if i have an AcceptanceCriteria class, i need to verify if it is accepted, rejected or pending.
I usually do it by returning numbers that represent the state, but it does not seem like a good form, it can be confusing.
for example:
class AcceptanceCriteria
{
const PENDING = 0;
const ACCEPTED = 1;
const REJECTED = 2;
protected $state = self::PENDING;
public function accept():void
{
$this->state = self::ACCEPTED;
}
public function reject():void
{
$this->state = self::REJECTED;
}
public function state():int
{
return $this->state;
}
}
I need to check the state frequently and also show it in the front, what is a better way to do it? I dont want to check in the front if and acceptance criteria state is 0, 1 or 2 for do something.
php oop
closed as primarily opinion-based by Paul, david, rickvdbosch, greg-449, Billal Begueradj Dec 31 '18 at 9:28
Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
What would be the correct way to handle the types of state that an object can have in an application?
For example, if i have an AcceptanceCriteria class, i need to verify if it is accepted, rejected or pending.
I usually do it by returning numbers that represent the state, but it does not seem like a good form, it can be confusing.
for example:
class AcceptanceCriteria
{
const PENDING = 0;
const ACCEPTED = 1;
const REJECTED = 2;
protected $state = self::PENDING;
public function accept():void
{
$this->state = self::ACCEPTED;
}
public function reject():void
{
$this->state = self::REJECTED;
}
public function state():int
{
return $this->state;
}
}
I need to check the state frequently and also show it in the front, what is a better way to do it? I dont want to check in the front if and acceptance criteria state is 0, 1 or 2 for do something.
php oop
What would be the correct way to handle the types of state that an object can have in an application?
For example, if i have an AcceptanceCriteria class, i need to verify if it is accepted, rejected or pending.
I usually do it by returning numbers that represent the state, but it does not seem like a good form, it can be confusing.
for example:
class AcceptanceCriteria
{
const PENDING = 0;
const ACCEPTED = 1;
const REJECTED = 2;
protected $state = self::PENDING;
public function accept():void
{
$this->state = self::ACCEPTED;
}
public function reject():void
{
$this->state = self::REJECTED;
}
public function state():int
{
return $this->state;
}
}
I need to check the state frequently and also show it in the front, what is a better way to do it? I dont want to check in the front if and acceptance criteria state is 0, 1 or 2 for do something.
php oop
php oop
asked Dec 31 '18 at 3:36
kmilo93sdkmilo93sd
89110
89110
closed as primarily opinion-based by Paul, david, rickvdbosch, greg-449, Billal Begueradj Dec 31 '18 at 9:28
Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as primarily opinion-based by Paul, david, rickvdbosch, greg-449, Billal Begueradj Dec 31 '18 at 9:28
Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
How about some accessors that return booleans rather than ints so your algorithm is completely encapsulated?
class AcceptanceCriteria
{
const PENDING = 0;
const ACCEPTED = 1;
const REJECTED = 2;
protected $state = self::PENDING;
public function accept():void
{
$this->state = self::ACCEPTED;
}
public function reject():void
{
$this->state = self::REJECTED;
}
// Accessors
public function is_rejected():bool
{
return self::PENDING == $this->state;
}
public function is_accepted():bool
{
return self::ACCEPTED == $this->state;
}
public function is_rejected():bool
{
return self::REJECTED == $this->state;
}
}
yes, is a better way to do it, but what happen if the class have more types of the states? inRevision, canceled, or something like this. Should in these case have a lot of accessors for the class?
– kmilo93sd
Dec 31 '18 at 4:08
1
Perhaps it could seem like it is getting bloated, but that is why you have a state management class. I worry about readability and maintainability more than the size of the class.if ( $state->is_accepted() ) { do_something(); }
is really readable, and a class full of one line functions like this is easy to maintain even without comments.
– JasonB
Dec 31 '18 at 4:16
I agree with what you say, it is much more readable, thank you very much.
– kmilo93sd
Dec 31 '18 at 4:20
add a comment |
Its good way to use strongly typed enums
. You can use splEnum or smart implementation from My C-Labs.
First move your states to separated enum class
<?php
use MyCLabsEnumEnum;
class AcceptanceCriteriaStateEnum extends Enum
{
private const PENDING = 0;
private const ACCEPTED = 1;
private const REJECTED = 2;
}
then you can modify your class like bellow
class AcceptanceCriteria
{
protected $state = AcceptanceCriteriaStateEnum::PENDING;
public function setState(AcceptanceCriteriaStateEnum $state):void
{
$this->state = $state;
}
public function getState():int
{
return $this->state;
}
public function isInState(AcceptanceCriteriaStateEnum $checkState):bool
{
return $this->state == $checkState;
}
}
for checking state you can use method isInState
which return boolean
$obj = new AcceptanceCriteria();
$obj->setState(AcceptanceCriteriaStateEnum::ACCEPTED);
// check status
echo $obj->isInState(AcceptanceCriteriaStateEnum::ACCEPTED);
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
How about some accessors that return booleans rather than ints so your algorithm is completely encapsulated?
class AcceptanceCriteria
{
const PENDING = 0;
const ACCEPTED = 1;
const REJECTED = 2;
protected $state = self::PENDING;
public function accept():void
{
$this->state = self::ACCEPTED;
}
public function reject():void
{
$this->state = self::REJECTED;
}
// Accessors
public function is_rejected():bool
{
return self::PENDING == $this->state;
}
public function is_accepted():bool
{
return self::ACCEPTED == $this->state;
}
public function is_rejected():bool
{
return self::REJECTED == $this->state;
}
}
yes, is a better way to do it, but what happen if the class have more types of the states? inRevision, canceled, or something like this. Should in these case have a lot of accessors for the class?
– kmilo93sd
Dec 31 '18 at 4:08
1
Perhaps it could seem like it is getting bloated, but that is why you have a state management class. I worry about readability and maintainability more than the size of the class.if ( $state->is_accepted() ) { do_something(); }
is really readable, and a class full of one line functions like this is easy to maintain even without comments.
– JasonB
Dec 31 '18 at 4:16
I agree with what you say, it is much more readable, thank you very much.
– kmilo93sd
Dec 31 '18 at 4:20
add a comment |
How about some accessors that return booleans rather than ints so your algorithm is completely encapsulated?
class AcceptanceCriteria
{
const PENDING = 0;
const ACCEPTED = 1;
const REJECTED = 2;
protected $state = self::PENDING;
public function accept():void
{
$this->state = self::ACCEPTED;
}
public function reject():void
{
$this->state = self::REJECTED;
}
// Accessors
public function is_rejected():bool
{
return self::PENDING == $this->state;
}
public function is_accepted():bool
{
return self::ACCEPTED == $this->state;
}
public function is_rejected():bool
{
return self::REJECTED == $this->state;
}
}
yes, is a better way to do it, but what happen if the class have more types of the states? inRevision, canceled, or something like this. Should in these case have a lot of accessors for the class?
– kmilo93sd
Dec 31 '18 at 4:08
1
Perhaps it could seem like it is getting bloated, but that is why you have a state management class. I worry about readability and maintainability more than the size of the class.if ( $state->is_accepted() ) { do_something(); }
is really readable, and a class full of one line functions like this is easy to maintain even without comments.
– JasonB
Dec 31 '18 at 4:16
I agree with what you say, it is much more readable, thank you very much.
– kmilo93sd
Dec 31 '18 at 4:20
add a comment |
How about some accessors that return booleans rather than ints so your algorithm is completely encapsulated?
class AcceptanceCriteria
{
const PENDING = 0;
const ACCEPTED = 1;
const REJECTED = 2;
protected $state = self::PENDING;
public function accept():void
{
$this->state = self::ACCEPTED;
}
public function reject():void
{
$this->state = self::REJECTED;
}
// Accessors
public function is_rejected():bool
{
return self::PENDING == $this->state;
}
public function is_accepted():bool
{
return self::ACCEPTED == $this->state;
}
public function is_rejected():bool
{
return self::REJECTED == $this->state;
}
}
How about some accessors that return booleans rather than ints so your algorithm is completely encapsulated?
class AcceptanceCriteria
{
const PENDING = 0;
const ACCEPTED = 1;
const REJECTED = 2;
protected $state = self::PENDING;
public function accept():void
{
$this->state = self::ACCEPTED;
}
public function reject():void
{
$this->state = self::REJECTED;
}
// Accessors
public function is_rejected():bool
{
return self::PENDING == $this->state;
}
public function is_accepted():bool
{
return self::ACCEPTED == $this->state;
}
public function is_rejected():bool
{
return self::REJECTED == $this->state;
}
}
answered Dec 31 '18 at 3:49
JasonBJasonB
4,00321021
4,00321021
yes, is a better way to do it, but what happen if the class have more types of the states? inRevision, canceled, or something like this. Should in these case have a lot of accessors for the class?
– kmilo93sd
Dec 31 '18 at 4:08
1
Perhaps it could seem like it is getting bloated, but that is why you have a state management class. I worry about readability and maintainability more than the size of the class.if ( $state->is_accepted() ) { do_something(); }
is really readable, and a class full of one line functions like this is easy to maintain even without comments.
– JasonB
Dec 31 '18 at 4:16
I agree with what you say, it is much more readable, thank you very much.
– kmilo93sd
Dec 31 '18 at 4:20
add a comment |
yes, is a better way to do it, but what happen if the class have more types of the states? inRevision, canceled, or something like this. Should in these case have a lot of accessors for the class?
– kmilo93sd
Dec 31 '18 at 4:08
1
Perhaps it could seem like it is getting bloated, but that is why you have a state management class. I worry about readability and maintainability more than the size of the class.if ( $state->is_accepted() ) { do_something(); }
is really readable, and a class full of one line functions like this is easy to maintain even without comments.
– JasonB
Dec 31 '18 at 4:16
I agree with what you say, it is much more readable, thank you very much.
– kmilo93sd
Dec 31 '18 at 4:20
yes, is a better way to do it, but what happen if the class have more types of the states? inRevision, canceled, or something like this. Should in these case have a lot of accessors for the class?
– kmilo93sd
Dec 31 '18 at 4:08
yes, is a better way to do it, but what happen if the class have more types of the states? inRevision, canceled, or something like this. Should in these case have a lot of accessors for the class?
– kmilo93sd
Dec 31 '18 at 4:08
1
1
Perhaps it could seem like it is getting bloated, but that is why you have a state management class. I worry about readability and maintainability more than the size of the class.
if ( $state->is_accepted() ) { do_something(); }
is really readable, and a class full of one line functions like this is easy to maintain even without comments.– JasonB
Dec 31 '18 at 4:16
Perhaps it could seem like it is getting bloated, but that is why you have a state management class. I worry about readability and maintainability more than the size of the class.
if ( $state->is_accepted() ) { do_something(); }
is really readable, and a class full of one line functions like this is easy to maintain even without comments.– JasonB
Dec 31 '18 at 4:16
I agree with what you say, it is much more readable, thank you very much.
– kmilo93sd
Dec 31 '18 at 4:20
I agree with what you say, it is much more readable, thank you very much.
– kmilo93sd
Dec 31 '18 at 4:20
add a comment |
Its good way to use strongly typed enums
. You can use splEnum or smart implementation from My C-Labs.
First move your states to separated enum class
<?php
use MyCLabsEnumEnum;
class AcceptanceCriteriaStateEnum extends Enum
{
private const PENDING = 0;
private const ACCEPTED = 1;
private const REJECTED = 2;
}
then you can modify your class like bellow
class AcceptanceCriteria
{
protected $state = AcceptanceCriteriaStateEnum::PENDING;
public function setState(AcceptanceCriteriaStateEnum $state):void
{
$this->state = $state;
}
public function getState():int
{
return $this->state;
}
public function isInState(AcceptanceCriteriaStateEnum $checkState):bool
{
return $this->state == $checkState;
}
}
for checking state you can use method isInState
which return boolean
$obj = new AcceptanceCriteria();
$obj->setState(AcceptanceCriteriaStateEnum::ACCEPTED);
// check status
echo $obj->isInState(AcceptanceCriteriaStateEnum::ACCEPTED);
add a comment |
Its good way to use strongly typed enums
. You can use splEnum or smart implementation from My C-Labs.
First move your states to separated enum class
<?php
use MyCLabsEnumEnum;
class AcceptanceCriteriaStateEnum extends Enum
{
private const PENDING = 0;
private const ACCEPTED = 1;
private const REJECTED = 2;
}
then you can modify your class like bellow
class AcceptanceCriteria
{
protected $state = AcceptanceCriteriaStateEnum::PENDING;
public function setState(AcceptanceCriteriaStateEnum $state):void
{
$this->state = $state;
}
public function getState():int
{
return $this->state;
}
public function isInState(AcceptanceCriteriaStateEnum $checkState):bool
{
return $this->state == $checkState;
}
}
for checking state you can use method isInState
which return boolean
$obj = new AcceptanceCriteria();
$obj->setState(AcceptanceCriteriaStateEnum::ACCEPTED);
// check status
echo $obj->isInState(AcceptanceCriteriaStateEnum::ACCEPTED);
add a comment |
Its good way to use strongly typed enums
. You can use splEnum or smart implementation from My C-Labs.
First move your states to separated enum class
<?php
use MyCLabsEnumEnum;
class AcceptanceCriteriaStateEnum extends Enum
{
private const PENDING = 0;
private const ACCEPTED = 1;
private const REJECTED = 2;
}
then you can modify your class like bellow
class AcceptanceCriteria
{
protected $state = AcceptanceCriteriaStateEnum::PENDING;
public function setState(AcceptanceCriteriaStateEnum $state):void
{
$this->state = $state;
}
public function getState():int
{
return $this->state;
}
public function isInState(AcceptanceCriteriaStateEnum $checkState):bool
{
return $this->state == $checkState;
}
}
for checking state you can use method isInState
which return boolean
$obj = new AcceptanceCriteria();
$obj->setState(AcceptanceCriteriaStateEnum::ACCEPTED);
// check status
echo $obj->isInState(AcceptanceCriteriaStateEnum::ACCEPTED);
Its good way to use strongly typed enums
. You can use splEnum or smart implementation from My C-Labs.
First move your states to separated enum class
<?php
use MyCLabsEnumEnum;
class AcceptanceCriteriaStateEnum extends Enum
{
private const PENDING = 0;
private const ACCEPTED = 1;
private const REJECTED = 2;
}
then you can modify your class like bellow
class AcceptanceCriteria
{
protected $state = AcceptanceCriteriaStateEnum::PENDING;
public function setState(AcceptanceCriteriaStateEnum $state):void
{
$this->state = $state;
}
public function getState():int
{
return $this->state;
}
public function isInState(AcceptanceCriteriaStateEnum $checkState):bool
{
return $this->state == $checkState;
}
}
for checking state you can use method isInState
which return boolean
$obj = new AcceptanceCriteria();
$obj->setState(AcceptanceCriteriaStateEnum::ACCEPTED);
// check status
echo $obj->isInState(AcceptanceCriteriaStateEnum::ACCEPTED);
answered Dec 31 '18 at 4:23
daremachinedaremachine
1,36121527
1,36121527
add a comment |
add a comment |