Jump to content

[SOLVED] Refer to another class


Jonob

Recommended Posts

New to php, but learning as I go.

 

Lets say I have a php file called user.php

class user
{
   function first();
   {
      Some code here
      return $result;
   }

   function second();
   {
      //I want to refer to function first
      $test = $this -> first()
      return $test
   }
}

 

OK cool, so if I want to refer to another function in the same class, then I use $this ->

 

Now, what happens if I have a second php file called (for example) company.php, and inside that file I have a class called company, and inside that class a function called get_company(). I want to use get_company() in user.php, so at the top of user.php I put

 

require_once("company.php")

 

Lets say I want to use the get_company function in the second() function. I've tried to reference it as

 $test = $this -> get_company()

but it throws an error.

 

I guess my question is: how do I reference a function in a different class?

 

Link to comment
https://forums.phpfreaks.com/topic/140503-solved-refer-to-another-class/
Share on other sites

Thanks guys, works perfectly  ;D

 

On the same topic, assuming the following:

-I have one class in a php file

-I have many functions in that class

-I need to reference other classes from other php files in only ONE of those functions

 

Is it considered OK coding practise to put the

require_once("filename.php") 

code inside that single function? Or should it be just after the opening

<?php

tag?

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.