how to handle the states of an object [closed]












0















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.










share|improve this 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.























    0















    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.










    share|improve this 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.





















      0












      0








      0








      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.










      share|improve this question














      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      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.


























          2 Answers
          2






          active

          oldest

          votes


















          1














          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;
          }


          }






          share|improve this answer
























          • 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



















          0














          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);





          share|improve this answer






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            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;
            }


            }






            share|improve this answer
























            • 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
















            1














            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;
            }


            }






            share|improve this answer
























            • 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














            1












            1








            1







            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;
            }


            }






            share|improve this answer













            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;
            }


            }







            share|improve this answer












            share|improve this answer



            share|improve this answer










            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



















            • 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













            0














            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);





            share|improve this answer




























              0














              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);





              share|improve this answer


























                0












                0








                0







                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);





                share|improve this answer













                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);






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 31 '18 at 4:23









                daremachinedaremachine

                1,36121527




                1,36121527















                    Popular posts from this blog

                    Monofisismo

                    Angular Downloading a file using contenturl with Basic Authentication

                    Olmecas