0

Im trying to understand objects & classes. But Im having a problem. Im trying to pass a variable from a class into another class.

Why Im doing this is mostly because I want to understand more how classs work but also for future where Im gonna need to send database connection into classes.

Here is my code simplified for the problem:

class databaseConnection
{
    public function connect(){

        return "localhost";

    }
}

class like
{

    private $database;

    public function __construct(){

        $this->database = databaseConnection::connect();

    }

    public function addLike()
    {
        return $database;
    }
}

$obj = new like;
echo $obj->addLike();

But this doesn't show anything. What i thought the results would be is echo "localhost";

Why isn't this working?

1
  • You should turn up error reporting, this would have helped. Commented Mar 17, 2014 at 8:41

5 Answers 5

1

connect is not a static method, you should either change it to static or create an instance.

// if you use databaseConnection::connect();
public static function connect(){

or

$db = new databaseConnection;
$this->database = $db->connect();

And you also need to change

public function addLike()
{ 
    // use $this to access object property
    return $this->database;
}
Sign up to request clarification or add additional context in comments.

Comments

1

You are calling databaseConnection::connect() as a static method. Modify it to:

public static function connect(){ }

Edit - as @Shankar Damodaran pointed, also add:

public function addLike()
{
    return $this->database;
}

3 Comments

Tried changing that but it still wont output anything
You need to make use of return $this->database; on your addLike()
Oh, your're right @ShankarDamodaran. I have missed it. :(
1

First of all you should really follow convention and start naming classes StartingWithCapitalLetter.

Secondly, "::" operator is used to call static methods (to put it simply - you don't have to create object of a class to call them, if they are public).

Normally, to call object's method you use operator "->", like $object->method(arguments); So in your case, you need to first create an object of your databaseConnection class (because you can't call methods on not-initialized methods) and then call "connect" on it, like that:

$connection = new databaseConnection();
$database = $connection->connect();

To pass a parameter, you need to modify the method declaration

public function connect($parameter){
    return "Connecting to " ... $parameter;
}

and call it with

$database = $connection->connect($parameter);

On a sidenote, you should really use parenthesis when creating objects of a class, like:

$obj = new like();
echo $obj->addLike();

Also, as deceze pointed out, you need to access class variable using $this instead of accessing local method variable:

public function addLike()
{
    return $this->database;
}

Comments

0
public function addLike()
{
    return $this->database;
}

Comments

0

$database and $this->database are two different variables. $database is a local function variable which does not exist, it's not the object property you set before.

Comments

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.