WhiteyDude Posted April 3, 2007 Share Posted April 3, 2007 I think this belongs here... If not, please move to the appropriate section Hi guys, I have a quick but relatively urgent question. I have 2 classes here, and I wish to call a function that's inside one of them from another. Example code: Main page: <?php $class1 = new Class1(); $class2 = new Class2(); $class1->function1(); ?> Class 1 <?php class Class1 { function Class1() {} function function1() { //THIS is the line I'm having a problem with Class2::class2function(); } } ?> Class 2 <?php class Class2 { function Class2() {} function class2function() { echo "Worked!"; } } ?> Another problem is that on the apache server I'm running (2.2.4, local) I'm unable to display PHP errors. I've tried everything I can think of - the PHP ini is starting to get worn, it's been modified so many times. PHP 5.2.1 by the way. Please help guys, I need this fixed within the next hour or so. Thanks, /Whitey Quote Link to comment https://forums.phpfreaks.com/topic/45365-cross-class-scripting/ Share on other sites More sharing options...
Daniel0 Posted April 3, 2007 Share Posted April 3, 2007 I think a method needs to be defined as static in order to call it using the scope resolution operator. Quote Link to comment https://forums.phpfreaks.com/topic/45365-cross-class-scripting/#findComment-220438 Share on other sites More sharing options...
per1os Posted April 3, 2007 Share Posted April 3, 2007 Along with Daniel0, you need to have class1 extend class2 in order to access functions from the other class. Quote Link to comment https://forums.phpfreaks.com/topic/45365-cross-class-scripting/#findComment-220546 Share on other sites More sharing options...
utexas_pjm Posted April 3, 2007 Share Posted April 3, 2007 Along with Daniel0, you need to have class1 extend class2 in order to access functions from the other class. That is not true. You can access methods from another class by calling them statically using the scope resolution operator or by instantiating Class2 within Class1. By having Class1 extend Class2 you are allowing Class1 to inherit all non private behaviors and attributes form Class2. This afford you the ability call these methods by using the "$this" pseudo variable. Best, Patrick Quote Link to comment https://forums.phpfreaks.com/topic/45365-cross-class-scripting/#findComment-220560 Share on other sites More sharing options...
utexas_pjm Posted April 3, 2007 Share Posted April 3, 2007 On a related note I was able to run the code you provided without any problems (output: "Worked!") using PHP 5.1.6. You can override whatever is in the ini file by putting this line in your code: <?php //.. error_reporting(E_ALL); //... ?> Or this, if you don't want notices: <?php //.. error_reporting(E_ALL ^ E_NOTICE); //... ?> Best, Patrick Quote Link to comment https://forums.phpfreaks.com/topic/45365-cross-class-scripting/#findComment-220619 Share on other sites More sharing options...
btherl Posted April 4, 2007 Share Posted April 4, 2007 Yes, the code posted runs perfectly. We need the actual code if we are going to debug it. Quote Link to comment https://forums.phpfreaks.com/topic/45365-cross-class-scripting/#findComment-220879 Share on other sites More sharing options...
WhiteyDude Posted April 4, 2007 Author Share Posted April 4, 2007 Already tried that utexas_pjm - doesn't do a thing . I know you really need the actual code, but what I'm working on is closed source currently... I'll show you as much as I can attack.php <?php ... require ($atw->getClassDirectory().'Fighting.class.php'); require ($atw->getClassDirectory().'Users.class.php'); ... $Fighting = new Fighting(); $Users = new Users(); ... if ($_POST['attack']) { $attackerData = $psuedo; $defenderData = $psuedo; $turns = $psuedo; $return = $Fighting->attackPlayer($attackerData,$defenderData,$turns); } ?> Fighting.class.php <?php class Fighting{ // constructor function Fighting(){} ... function attackPlayer($attackerData,$defenderData,$turns) { ... $Users::updateUser($attackerData['userID'], "updatedata=here"); $Users::updateUser($defenderData['userID'], "updatedata=here"); return true; } ?> Users.class.php <?php class Users { // constructor function Users(){ } ... function updateUser($id, $data) { //Update code here } ?> Also, I'm not really a big O.O.P. buff (obviously). That link Daniel0 posted makes me think the second class (Users) has to be defined as a child class? I hope not, because I still want to be able to call it without calling Fighting... Thanks guys, /Whitey Quote Link to comment https://forums.phpfreaks.com/topic/45365-cross-class-scripting/#findComment-220929 Share on other sites More sharing options...
btherl Posted April 4, 2007 Share Posted April 4, 2007 No, there's no need to make anything a child class of anything. Static method calls can be made from anywhere to any object, and will work fine as long as the method doesn't reference $this. That's demonstrated by your example code. The code you just posted doesn't seem to match your example though. In the code you posted $Users::updateUser($attackerData['userID'], "updatedata=here"); $Users is an instance of the Users object. But :: is for making direct calls to a class, without a particular instance. Using code like this gives me an error here with php 5.1.4 Class2::class2function(); This says to call class2function() from Class2, with no instance. This is fine as long as class2function() does not mention $this $c = new Class2(); $c->class2function(); This says to call class2function() from the $c instance of Class2. Since there is an object instance given, it's fine for class2function() to reference $this (and you would expect it to in this case). Quote Link to comment https://forums.phpfreaks.com/topic/45365-cross-class-scripting/#findComment-220977 Share on other sites More sharing options...
redbullmarky Posted April 4, 2007 Share Posted April 4, 2007 You can override whatever is in the ini file by putting this line in your code: <?php //.. error_reporting(E_ALL); //... ?> on a semi-related note, open up your php.ini file and make the relevent error level/setting changes there (error_reporting and display_errors). if you have some sort of typo or fatal error in your script, then the above wont work/display errors as it'll never get run itself. Quote Link to comment https://forums.phpfreaks.com/topic/45365-cross-class-scripting/#findComment-221018 Share on other sites More sharing options...
WhiteyDude Posted April 4, 2007 Author Share Posted April 4, 2007 Oh... Oh my . Thank you btherl . I was doing $User instead of User . Works fine now . And redbullmarky, I know . However, changing the settings in the ini doesn't do it either. Here's some of my php.ini... error_reporting = E_ALL & E_STRICT display_errors = On display_startup_errors = On log_errors = On log_errors_max_len = 1024 ignore_repeated_errors = Off ignore_repeated_source = Off report_memleaks = On track_errors = On error_prepend_string = "<font color=ff0000>" error_append_string = "</font>" error_log = "C:\error.log" /Whitey Quote Link to comment https://forums.phpfreaks.com/topic/45365-cross-class-scripting/#findComment-221021 Share on other sites More sharing options...
Jenk Posted April 4, 2007 Share Posted April 4, 2007 Note that using the scope resolution operator superflously is a code smell; you should be passing your objects (example, you user object) around as a parameter instead of using the pseudo global that is the scope resolution operator. There are a few circumstances where the SRO is warranted, but this is certainly not one of them. Quote Link to comment https://forums.phpfreaks.com/topic/45365-cross-class-scripting/#findComment-221081 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.