Warning: Constant ABSPATH already defined, this will be an error in PHP 9 in /home/killerph/public_html/wp-config.php on line 19

Warning: Cannot modify header information - headers already sent by (output started at /home/killerph/public_html/wp-config.php:19) in /home/killerph/public_html/wp-includes/feed-rss2-comments.php on line 8
Comments on: How to call functions from another class. https://www.killerphp.com/articles/how-to-call-functions-from-another-class/ Thu, 09 Dec 2021 20:55:02 +0000 hourly 1 https://wordpress.org/?v=6.4.13 By: Stefan Mischook https://www.killerphp.com/articles/how-to-call-functions-from-another-class/#comment-622 Fri, 17 Jul 2009 01:32:11 +0000 http://www.killerphp.com/articles/?p=149#comment-622 Hi,

Can you repost your question on the PHP forum:

http://www.killersites.com/forums/forum/12/php/

Thanks,

Stefan

]]>
By: Rob https://www.killerphp.com/articles/how-to-call-functions-from-another-class/#comment-621 Thu, 16 Jul 2009 12:39:59 +0000 http://www.killerphp.com/articles/?p=149#comment-621 I have a possibly similar question about already instantiated classes. Based on what I have seen on other sites I have something like this:

—class.login.php
class login
{
var $loginID; var $role; ….
function getCurrentLogin() {$this->loginID=…. }

function displayUserOptions() {echo “Welcome “.this->name …}
}

—class.navigation.php
class navigation
{
var currentPage; var menuStructure; ….

function displayNavigation($currentLogin)
{
code to display menus;
$currentLogin->displayUserOptions();
}

—-index.php
include(class.login.php);
include(class.navigation.php);
if(!isset($_SESSION[“$currentLogin”])){
$_SESSION[“$currentLogin”] = new login;}
$_SESSION[“$currentLogin”]->checkOrGetCurrentLogin();

if(!isset($_SESSION[“$navigation”])){
$_SESSION[“$navigation”] = new navigation;}
$_SESSION[“$navigation”]->displayNavigation($_SESSION[“$currentLogin”])

The navigation bar displays fine then I get Call to undefined method login::displayUserOptions()

]]>
By: Stefan Mischook https://www.killerphp.com/articles/how-to-call-functions-from-another-class/#comment-620 Thu, 22 Jan 2009 23:14:11 +0000 http://www.killerphp.com/articles/?p=149#comment-620 Glad I could help!!

😉

Stefan

]]>
By: Eric https://www.killerphp.com/articles/how-to-call-functions-from-another-class/#comment-619 Thu, 22 Jan 2009 22:53:05 +0000 http://www.killerphp.com/articles/?p=149#comment-619 You know what? I don’t need to use $this when referencing variables passed into the methods in classes. I think by removing “this->”, the code example will work. I answered my own question 😀

Thanks again!

]]>
By: Eric https://www.killerphp.com/articles/how-to-call-functions-from-another-class/#comment-618 Thu, 22 Jan 2009 22:47:17 +0000 http://www.killerphp.com/articles/?p=149#comment-618 Thank you for this tutorial. My question is, how do you call a function from another class if the classes are both instantiated from outside the classes (out at index.php, for example)?

For example, I have my class “website”, which executes session_start() among other things. My website class also houses encryption methods.

In my “pages” class, which I instantiate to create a page, it needs access to these encryption methods.

So these are instantiated from index.php, and I can’t instantiate website class twice, since it runs code that should only be executed once per page load.

I could pass my “website” object path when instantiating “pages”, but the code:

class page
{
public $website
function __construct($website)
{
$this->website->encrypt();
}
}

Fails to work. How would one get around this?

]]>