2

I'm writing my own php class and I have several functions within that class. But am I not allowed to call a function from another function within the same class? Something like this:

class my_Class {
    function one($arg) {
        //does something
    }

    function two($var) {
        $receive = one($var);
    }
}

I tried something like this and it spat out an error saying:

Fatal error: Call to undefined function one()

What am I doing wrong?

2
  • 2
    If you're coming from Java or C++, note that the use of $this in PHP (aka self in some other languages) is mandatory. Commented Oct 4, 2012 at 22:37
  • @Radu yah i'm fresh of the c++ boat :P thank you for the help Commented Oct 4, 2012 at 22:39

2 Answers 2

6

Change it to this:

function two($var) {
      $receive = $this->one($var);
 }

Review the PHP OOP reference: http://www.php.net/manual/en/language.oop5.php
The $this keyword is always required.

Sign up to request clarification or add additional context in comments.

1 Comment

Yup, this is exactly what you need to change it too.
2

It should be

class my_Class {
    function one($arg) {
        // does something
    }
    function two($var) {
        $receive = $this->one($var);
    }
}

2 Comments

@Radu .. i think you should do some test one() would not be global see codepad.viper-7.com/Q85Tp4
@Radu ok valid argument it can also lead to Fatal error: Cannot redeclare one() .. i'll remove that option +1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.