Power of PHP: Return a value

Last time we provided the function within a class with some text provided by the user and then it did something with this. But what if we wanted a function within a class to return a value that we can then present and manipulate?

In order to do this we'll create a class with three elements, the first of which is a public variable and the other two of which are functions.

First of all the public variable needs to be declared within the class like this:

//Create a public variable
public $data;

Next we're going to supply a method by which the public variable can attain a value:

function addData ($newdata) {
//Add a value to the public variable
$this->data=$newdata;
}

Finally the class will do something with this value:

function returnData () {
//Return the value of the public variable + 10
return $this->data+10;
}

The final function adds 10 to whatever data is provided, which isn't anything dramatic but it demonstrates the point.

Our entire class therefore reads like this:

class returnValue {
//Create a public variable
public $data;
function addData ($newdata) {
//Add a value to the public variable
$this->data=$new-data;
}
 function returnData () {
//Return the value of the public variable + 10
return $this->data+10;
}
}

Now we could and probably would in a real-world scenario place this class in a separate file and use "include" as we did last time with livetext.php, but for convenience we're going to bundle this in the same file as the instantiation of the object.

So our whole file looks like this:

<?php class returnValue {
//Create a public variable
public $data;
function addData ($newdata) {
//Add a value to the public variable
$this->data=$newdata;
}
function returnData () {
//Return the value of the public variable + 10
return $this->data+10;
}
}
//Create an instance of the class
$newClassyInstance = new returnValue();
//Run the addData function contained within the class (whatever is contained with the parentheses will be used)
$newClassyInstance->addData(10);
//Return the result
echo $newClassyInstance->returnData() . ", ";
//Return the public variable $data. Note this couldn't be printed if it was declared private
echo $newClassyInstance->data; ?>

But let's take a closer look at the final lines of code. First of all the instantiation of the object:

//Create an instance of the class
$newClassyInstance = new returnValue();

This occurs before we supply data to the first function:

//Run the addData function contained within the class (whatever is contained with the parentheses will be used)
$newClassyInstance->addData(10);

We then return the result using the second function and just for the fun of it we print the value of the public variable.

//Return the result
echo $newClassyInstance->returnData() . ", ";
//Return the public variable $data. Note this couldn't be printed if it was declared private
echo $newClassyInstance->data; ?>

If we had written private or protected instead of public in front of the $data variable inside the class we wouldn't have been able to implement this final line of code because the variable would have been invisible to the code outside the class. Try it!

Comments