0

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:

Sphere.class.php

   <?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.

App.class.php

 <?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();

    ?>
Community
  • 1
  • 1
  • Note: You can use the `M_PI` constant or the `pi()` function to get a more accurate number – Sverri M. Olsen May 02 '16 at 04:12
  • Consider usage of traits. http://php.net/manual/en/language.oop5.traits.php – Pinke Helga May 02 '16 at 04:20
  • Or are you just looking for class extension / derivation? – Pinke Helga May 02 '16 at 04:24
  • I'm not too sure but I think derivation. So it pulls the function from the source then the input of variables and output is in the second. – cigarette_unicorn May 02 '16 at 04:28
  • imo, The way the question is phrased is that extending another class is what you want ('extends') imo, However, are there other options that don't use 'class extends' but makes it easy to try different approaches. What about an interface? This is contract that every class must provide. How, not our problem - well it is.... Imagine you don't have one class that calculates `Area from a radius` but two! i.e. Sphere and Circle. So, Can you have one class `AppClass` work with both of them without changing any of the classes? maybe interesting? https://eval.in/564694 – Ryan Vincent May 03 '16 at 18:41
  • 1
    @Ryan Vincent Oooh that is interesting! It's somewhat understandble for my beginner brain too. Pretty cool! – cigarette_unicorn May 03 '16 at 19:40
  • Your welcone - enjoy:) – Ryan Vincent May 03 '16 at 19:46

3 Answers3

1

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

?>
0

If you want to use some methods of a class in another one, you have to extend that class. Use parent:: or ClassName:: to call a method of the anchestor.

You can and should write every class in a separate file. Just use require_once to load a class needed in another file. It would be applicable to include most commonly used classes of a project in one place or even better have a look at spl_autoload mechanism.

Since there was no classic polymorphism in PHP, PHP 5.4 has introduced traits. Now you can declare "packages" of methods that can be inherited by different class families.

<?php
class BaseClass
{
  protected 
    $a
  ;

  public function __construct($a)
  {
    $this->a = $a;
  }

  public function do_something()
  {
    echo $this->a . "<br>\n";
  }  
}

class Derived extends BaseClass
{
  protected 
    $b
  ;

  public function __construct($a, $b)
  {
    parent::__construct($a);
    $this->b = $b;
  }

  public function do_something()
  {
    echo $this->a . ' / ' . $this->b . "<br>\n";
  }
}

class Derived2 extends Derived
{
  public function do_something()
  {
    parent::do_something();
    BaseClass::do_something();
  }  
}

$obj = new Derived2(1,2);
$obj->do_something();
?>
Pinke Helga
  • 6,378
  • 2
  • 22
  • 42
0

It looks to me like you simply want to use (i.e. instantiate) your class; that this should happen within another class is relatively unimportant.

include 'Sphere.class.php';

class AppClass {

    protected $radius;

    function __construct($radius) {
        $this->radius = $radius;
    }

    function callSphereClass() {
        $sphere = new SphereCalculator($this->radius);
        echo "The area is ", $sphere->getArea();
    }
}


$newRadius = 113;
$appClass = new AppClass($newRadius);
$appClass->callSphereClass();
deceze
  • 510,633
  • 85
  • 743
  • 889