0

Currently having problems calling functions from other PHP classes in the same folder. I have instantiated the class and everything, and it still doesn't work for me. The error message I get when calling a function from the first class is:

method getHello() not found in class.

Hope anyone know how to fix this issue, believe I have done something wrong somewhere. Thank you very much in advance, below is the code for both php files.

<?php
class one 
{

    private $hello;

    public function __construct()
    {
        $this->hello = '';
    }

    public function getHello()
    {
        return $this->hello;
    }

    public function setHello($newHello)
    {
        $this->hello = $newHello;
    }
}
?>

and the second class

<?php

class two 
{

    private $sixPack;

    public function __contruct()
    {
        $this->sixPack  = new one();
    }

    public function setSixPack($newSixPack)
    {
        $this->sixPack = $newSixPack;
    }

    public function getSixPack()
    {
        return $this->sixPack;
    }

    public function access_function_one_methods()
    {
        // this is causing the problem, cannot access getHello

        $sad = $this->sixPack->getHello();
    }
}
?>
jeroen
  • 91,079
  • 21
  • 114
  • 132
Jordan
  • 327
  • 8
  • 23
  • why don't use extends – Robert Mar 05 '14 at 22:35
  • How are you calling your methods, have you perhaps used the setter of class `two` and overwritten the variable with something else? – jeroen Mar 05 '14 at 22:44
  • @robert I have tried extends, it still doesn't make any difference..This is what I tried `class one extends two` – Jordan Mar 05 '14 at 22:45
  • what the error message ? because i use the code but there are no error and i just add `$row=new two();` – Robert Mar 05 '14 at 22:49
  • @jeroen I have instantiated the class one.php inside the constructor two.php, so I expect it to give me complete access to all public defined methods inside one.php , I am right at that point ain't I? – Jordan Mar 05 '14 at 22:49
  • Actually this is getting fun, i cant make it work lol – Jorge Y. C. Rodriguez Mar 05 '14 at 22:51
  • @robert can you see the last line of code of the second class!! So it would allow me to access getHello?, if I hover it, it would say method getHello not found in class – Jordan Mar 05 '14 at 22:53
  • 1
    @Jordan Correct, your code runs just fine / without any errors when you fix the typo in the constructor and include both files: http://codepad.viper-7.com/EEgxcO – jeroen Mar 05 '14 at 22:54
  • 2
    By the way, you should post the **complete** error message and not just an extract. – jeroen Mar 05 '14 at 23:01
  • @Jordan i use `$two=new two(); $two->access_function_one_methods();` and here the error ( ! ) Fatal error: Call to a member function getHello() on a non-object in C:\wamp\www\New folder\submit\upload.php on line 49 – Robert Mar 05 '14 at 23:04
  • @robert that's because of the typo in the constructor. Change it to __construct, and that error should disappear – Prathap Mar 05 '14 at 23:08
  • 1
    @jeroen Thanks for looking into it, seemed that the typo was the issue!! Cheers guys – Jordan Mar 05 '14 at 23:11

2 Answers2

1

This seems to work, im not sure if it is what you want it, but give it a try:::

file for class two::

            private $sixPack;

            public function __construct()
            {
                include_once('/Users/hakunamatata/Desktop/one.php');
                $this->sixPack  = new one();
                print("second construct \n");
            }

            public function setSixPack($newSixPack)
            {
                $this->sixPack = $newSixPack;
            }

            public function getSixPack()
            {
                return $this->sixPack;
            }

            public function access_function_one_methods()
            {
                // this is causing the problem, cannot access getHello
                print("function calling class one \n");
                $sad = $this->sixPack->getHello();
                echo $sad;
            }
          }
        $two = new two();
        echo $two->access_function_one_methods();
?>

file for class one::

<?php
    class one {

                    private $hello = "Je";
                    public function __construct()
                    {
                        print("first construct \n");
                        $this->hello = 'Hello';
                    }

                    public function getHello()
                    {
                        echo "we are here \n";
                        return $this->hello;
                    }

                    public function setHello($newHello)
                    {
                        $this->hello = $newHello;
                    }
                 }

?>

console output:::

first construct 
second construct 
function calling class one 
we are here 
Hello
Jorge Y. C. Rodriguez
  • 3,394
  • 5
  • 38
  • 61
0

is that a spello in the constructor? perhaps call it __construct()? (missing an s?)

Prathap
  • 474
  • 1
  • 6
  • 19