PHPackages                             mrferrys/finitestatemachine - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. mrferrys/finitestatemachine

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

mrferrys/finitestatemachine
===========================

A Finite State Machine System based on Chapter 3.1 of Game Programming Gems 1 by Eric Dybsand. Written by Roberto Cezar Bianchini, July 2010. Ported to PHP by MrFerrys

1.0.0(4y ago)528proprietaryPHPPHP &gt;=5.0.0

Since Jan 30Pushed 4y ago1 watchersCompare

[ Source](https://github.com/MrFerrys/finitestatemachine)[ Packagist](https://packagist.org/packages/mrferrys/finitestatemachine)[ RSS](/packages/mrferrys-finitestatemachine/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (2)Used By (0)

finitestatemachine
==================

[](#finitestatemachine)

A Finite State Machine System based on Chapter 3.1 of Game Programming Gems 1 by Eric Dybsand,Written by Roberto Cezar Bianchini, July 2010 ported to php by MrFerrys.

Description
-----------

[](#description)

A Finite State Machine System based on Chapter 3.1 of Game Programming Gems 1 by Eric Dybsand,Written by Roberto Cezar Bianchini, July 2010 ported to php by MrFerrys.

Getting Started
---------------

[](#getting-started)

### Dependencies

[](#dependencies)

- PHP &gt;= 5

### Installation

[](#installation)

composer require mrferrys/finitestatemachine

### Usage

[](#usage)

- How to use it.

```
	use mrferrys\finitestatemachine\finiteStateMachineClass as fsmClass;
	use mrferrys\finitestatemachine\stateClass as stateClass;

	class State_1 extends stateClass
	{

		function __construct($id)
		{
		   $this->stateID=$id;
		}
		public function DoBeforeEntering(){
			echo "StateID: $this->stateID DoBeforeEntering ";
		}

		public function DoBeforeLeaving(){
			echo "StateID: $this->stateID DoBeforeLeaving ";
		}

		public function Reason(){
			echo "StateID: $this->stateID Reason ";
			if($this->fsm!=null)
			{
				$this->fsm->PerformTransition($this->stateID);
			}
		}

		public function Act(){
			echo "StateID: $this->stateID ACTION ";
		}
	}

	class stateMachine{
	public $fsm;
	public $states=array(
		"nullState"=>0,
		"state_1"=>1,
		"state_2"=>2,
		"finish"=>3
		);
	public $transitions=array(
		"nullTransition"=>0,
		 "state_1_done" => 1,
		 "state_2_done"=> 2,
		 "finishing"=>3,
		);
		function __construct()
		{

		}

		public function buildFSM(){
			$this->fsm =   new fsmClass();
			$s1         =   new State_1($this->states["state_1"]);
			$s1->fsm    =   $this->fsm;
			$s1->AddTransition($this->transitions["state_1_done"],$this->states["state_2"]);

			$s2         =   new State_1($this->states["state_2"]);
			$s2->fsm    =   $this->fsm;
			$s2->AddTransition($this->transitions["state_2_done"],$this->states["finish"]);

			$s3  =   new State_1($this->states["finish"]);
			$s3->fsm    = $this->fsm;
			$this->fsm->AddState($s1);
			$this->fsm->AddState($s2);
			$this->fsm->AddState($s3);
		}

		public function startProcess(){

			while($this->fsm!=null && $this->fsm->getCurrentStateID() != $this->states["finish"] )
			{
				$this->fsm->getCurrentState()->Reason();
				$this->fsm->getCurrentState()->Act();
				$this->fsm->PerformTransition($this->fsm->getCurrentStateID());
			}
		}

	}
	//MAIN
	$machine= new stateMachine();
	$machine->buildFSM();
	$machine->startProcess();

```

Authors
-------

[](#authors)

MrFerrys

Version History
---------------

[](#version-history)

- 1.0.0
    - Initial Release (X.Y.Z MAJOR.MINOR.PATCH)

License
-------

[](#license)

Finite State Machine A Finite State Machine System based on Chapter 3.1 of Game Programming Gems 1 by Eric Dybsand

Written by Roberto Cezar Bianchini, July 2010

How to use: 1. Place the labels for the transitions and the states of the Finite State System in the corresponding enums.

```
2. Write new class(es) inheriting from FSMState and fill each one with pairs (transition-state).
    These pairs represent the state S2 the FSMSystem should be if while being on state S1, a
    transition T is fired and state S1 has a transition from it to S2. Remember this is a Deterministic FSM.
    You can't have one transition leading to two different states.

   Method Reason is used to determine which transition should be fired.
   You can write the code to fire transitions in another place, and leave this method empty if you
   feel it's more appropriate to your project.

   Method Act has the code to perform the actions the NPC is supposed do if it's on this state.
   You can write the code for the actions in another place, and leave this method empty if you
   feel it's more appropriate to your project.

3. Create an instance of FSMSystem class and add the states to it.

4. Call Reason and Act (or whichever methods you have for firing transitions and making the NPCs
     behave in your game) from your Update or FixedUpdate methods.

Asynchronous transitions from Unity Engine, like OnTriggerEnter, SendMessage, can also be used,
just call the Method PerformTransition from your FSMSystem instance with the correct Transition
when the event occurs.

```

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Ported to PHP by MrFerrys Author Roberto Cezar Bianchini

See the LICENSE file for details

Acknowledgments
---------------

[](#acknowledgments)

Finite State Machine:

- [programmerclick.com](https://programmerclick.com/article/73042375109/)
- [wiki.unity3d.com](https://wiki.unity3d.com/index.php?title=Finite_State_Machine)

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity43

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

1559d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/94b2b10811470824dce9ee4a92d901935f28524678047e4a2e9a58542830f6cf?d=identicon)[MrFerrys](/maintainers/MrFerrys)

---

Top Contributors

[![MrFerrys](https://avatars.githubusercontent.com/u/866868?v=4)](https://github.com/MrFerrys "MrFerrys (4 commits)")

### Embed Badge

![Health badge](/badges/mrferrys-finitestatemachine/health.svg)

```
[![Health](https://phpackages.com/badges/mrferrys-finitestatemachine/health.svg)](https://phpackages.com/packages/mrferrys-finitestatemachine)
```

###  Alternatives

[pwm/datetime-period

An implementation of the datetime period type for working with temporal intervals

679.0k](/packages/pwm-datetime-period)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
