Jump to content

How do I use a function from another class?


jamesxg1

Recommended Posts

Hiya peeps!

 

I was wondering how I could use a function from one class in another EG.

 

class_file_one.php

class a {
function a() {
return true;
}
}

 

class_file_two.php

 

class b {
function b() {
if($this->a()) {
return 'it works';
}
}
}

 

The classes are in completely different pages.

 

Many thanks,

 

James.

It depends if both are related then you could do:

 

require 'a.php';

class b extends a

 

If they are not then you must ask yourself will a exist outside of b? Yes then

 

require 'a.php';

class b {
  function __construct(a $a)

 

otherwise:

 

require 'a.php';

class b {
  function __construct() {
      $this->a = new a();
  }
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.