34,333
edits
Changes
→Class Constructor and Destructor
We have now defined a class. The next step is add some functionality to the class.
== PHP Class Constructor Constructors and Destructor Destructors ==
The next step in creating a class is to define what should happen when an object is first instantiated using the class, and also when that object is later destroyed. These actions are defined the ''constructor'' and ''destructor'' methods of the class.
The constructor and destructor are really just functions that get called when the object is created and destroyed and are defined in the class body using the ''function'' keyword. This needs to be prefixed witwith the ''public'' qualifier. This means the method is accessible to code outside of the object. The default names fo rthe constructor and destructor are __construct and __destruct respectively. We can now expend out bankAccount class: <pre>