Sasuun Posted July 10, 2006 Share Posted July 10, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/14243-class-error-calling-a-member-function-from-a-non-member-object/ Share on other sites More sharing options...
Prismatic Posted July 11, 2006 Share Posted July 11, 2006 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 Link to comment https://forums.phpfreaks.com/topic/14243-class-error-calling-a-member-function-from-a-non-member-object/#findComment-55949 Share on other sites More sharing options...
Sasuun Posted July 11, 2006 Author Share Posted July 11, 2006 [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 Quote Link to comment https://forums.phpfreaks.com/topic/14243-class-error-calling-a-member-function-from-a-non-member-object/#findComment-55971 Share on other sites More sharing options...
willfitch Posted July 11, 2006 Share Posted July 11, 2006 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 classclass UserSettings { public $password; public function changePassword($pass) { $this->password = $pass; }}// Parent classclass 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? Quote Link to comment https://forums.phpfreaks.com/topic/14243-class-error-calling-a-member-function-from-a-non-member-object/#findComment-55982 Share on other sites More sharing options...
Sasuun Posted July 11, 2006 Author Share Posted July 11, 2006 thats exactly what I was wondering, thanks, solved Quote Link to comment https://forums.phpfreaks.com/topic/14243-class-error-calling-a-member-function-from-a-non-member-object/#findComment-56482 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.