Jump to content


Photo

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


  • Please log in to reply
4 replies to this topic

#1 Sasuun

Sasuun
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 10 July 2006 - 11:40 PM

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

#2 Prismatic

Prismatic
  • Members
  • PipPipPip
  • Advanced Member
  • 503 posts
  • LocationSan Diego

Posted 11 July 2006 - 02:01 AM

You creating the object before attempting to use it?

<?php $core = new ClassName; ?>

Now you would be able to call your methods

#3 Sasuun

Sasuun
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 11 July 2006 - 02:42 AM

You creating the object before attempting to use it?

<?php $core = new ClassName; ?>

Now you would be able to call your methods

hm... I'm doing that, it seems I'll have to find my test code to show you guys what I mean

#4 willfitch

willfitch
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts
  • LocationNew Hope, PA

Posted 11 July 2006 - 03:24 AM

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:

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

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?
Zend Certified Engineer (ZEND001989)
www.willfitch.com

#5 Sasuun

Sasuun
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 11 July 2006 - 10:50 PM

thats exactly what I was wondering, thanks, solved




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users