Apologies if my terminology isn't correct. I would like to use methods/functions from one class and use them to output variables from another class file. This is my main base:
<?php
class SphereCalculator {
const PI = 3.14;
const FOUR_THIRDS = 1.33;
public function __construct($radius){
$this->setRadius ($radius);
}
public function setRadius ($radius){
$this->classRadius = $radius;
}
public function getRadius(){
return $this->classRadius;
}
public function getArea () {
return self::PI * ($this->classRadius * $this->classRadius);
}
}
$mySphere = new SphereCalculator ($newRadius);
And so using this class's functions, I would like to output the radius and area from a second php file using include
to pull over the methods. But I'm really clueless as to where to start. I have looked up many tutorials but they are all for two classes with the one php file. Here is as far as I have gotten.
<?php
include ("Sphere.class.php");
class AppClass {
function __construct($radius)
{
//something here to call $radius from
//base class and set it to $newRaduis
}
function callSphereClass ($mySphere)
{
//something to call $mySphere
//something here to allocate $newRadius
echo "The radius is ".$newRadius."<br>";
echo "The area is ".$mySphere->getArea ()."<br>";
}
}
$newRadius = 113;
$appClass = new AppClass ();
$appClass->callSphereClass();
?>
HI you could do this.
<?php
include ("Sphere.class.php");
class AppClass {
function __construct()
{
// YOU DO NOT NEED ANYTHING HERE BECAUSE YOU ARE NOT PASSING
// ANY VALUES INTO THE CONSTRUCTOR
}
function callSphereClass ($mySphere)
{
//something to call $mySphere
// CREATE AN INSTANCE OF SphereCalculator
$createdObj = new SphereCalculator($mySphere);
//something here to allocate $newRadius
// USE THE METHODS THROUGH THE OBJECT $createdObj
echo "The radius is ".$createdObj->getRadius() ."<br />"; // not $newRadius."<br>";
echo "The area is ".$createdObj->getArea ()."<br>";
}
}
$newRadius = 113; // this needs to be passed into the callSphereClass()
$appClass = new AppClass();
$appClass->callSphereClass($newRadius); // like this
?>