Beginning Object Oriented Programming in PHP - Methods
(Page 4 of 6 )
To ask the box what it contains, the special get_whats_inside function was used. Functions defined in the class are known as methods; they act as a method for communicating with and manipulating the data within the box.
The nice thing about methods is that they allow us to separate all the class coding from our actual script.
We could save all of the class code in a separate file and then use include to import it into our script. Our scripts become more streamlined and, because we used descriptive names for our methods, anyone else reading our code can easily see our train-of-thought.
<?php include("class.Box.php");
$mybox = new Box("Jack"); echo $mybox->get_whats_inside();
?> ?>
Another benefit of methods is that it provides our box or whatever other objects we may build with a standard interface that anyone can use. We can share our classes with other programmers or even import them into our other scripts when the functionality they provide is needed.
<?php include("class.Box.php");
$mybox = new Box("Suggestion"); echo $mybox->get_whats_inside(); ?>
<?php include("class.Box.php");
$mybox = new Box("Shoes"); echo $mybox->get_whats_inside(); ?>