Jump to content

Class error; calling a member function from a non member object


Sasuun

Recommended Posts

Hey, I'm try to create a series of classes for my website where each function is seperated into it's own class group, and I can call them into each other easily.

For example, my user class will contain functions for checking all kinds of user stuff from a mysql database, which I would like to access via my mysql class. Basically what I'm trying to do is tohave one base class to contain all of my other classes, so lets call my base class $core, I would like to have my user class be $core->user->function_to_execute, and I would like to, within my classes be able to go $this->{classname}->{function}. I've seen this done in many scripts, like ipb and phpbb, but I can't figure out how to avoid that anoying fatal error: called member function from a non member object. thanks in advance
Link to comment
Share on other sites

[quote author=Prismatic link=topic=100131.msg394855#msg394855 date=1152583303]
You creating the object before attempting to use it?

[code]
<?php $core = new ClassName; ?>
[/code]

Now you would be able to call your methods
[/quote]
hm... I'm doing that, it seems I'll have to find my test code to show you guys what I mean
Link to comment
Share on other sites

The error PHP is giving you is what it says it is.  You are trying to call members of a class on an item that's not an object.  In this case $core->user->function(), user is not an object, so you can't call methods and properties on that.  This is known as dereferencing, not to be confused by C and C++ dereferencing which is accessing a pointer, which accesses an address in memory.  Here is an example of what you are looking for:

[code=php:0]
<?php
// First class we create is accessed from within the UserManagement class
class UserSettings {
      public $password;
      public function changePassword($pass) {
            $this->password = $pass;
      }
}
// Parent class
class UserManagement {
      public $user;
      public function __construct() {
            $this->user = new UserSettings();
      }
}
$core = new UserManagement();
$core->user->changePassword('newpassword');
?>
[/code]

You see, dereferencing allows properties, and even methods to have their own set of properties and methods, eliminating the need to instantiate tons of different classes.  BE AWARE that creating many classes to be accessed by a parent class will affect performance. 

Does this clear things up?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.