Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/45365-cross-class-scripting/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/45365-cross-class-scripting/#findComment-220560
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/45365-cross-class-scripting/#findComment-220619
Share on other sites

Already tried that utexas_pjm - doesn't do a thing :P.

 

 

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

Link to comment
https://forums.phpfreaks.com/topic/45365-cross-class-scripting/#findComment-220929
Share on other sites

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).

Link to comment
https://forums.phpfreaks.com/topic/45365-cross-class-scripting/#findComment-220977
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/45365-cross-class-scripting/#findComment-221018
Share on other sites

Oh... Oh my :P.

 

 

Thank you btherl :P.

 

 

I was doing $User instead of User -_-.

 

 

Works fine now :D.

 

 

And redbullmarky, I know :P. 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

Link to comment
https://forums.phpfreaks.com/topic/45365-cross-class-scripting/#findComment-221021
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/45365-cross-class-scripting/#findComment-221081
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.