Jump to content

varible is empty when I call a function from a class


dannyman1234

Recommended Posts

Hi im a beginner in OOP php5 and I hope someone can help.

 

I made a class House

 

class House {

 

public $AI;

private $MAP;

 

 

public function getAI($photo_AI) {

return $this->AI;

}

 

public function getMAP() {

}

 

}

 

THen in index.php

 

require 'phpclasses/class_lib.php';

 

 

$house = new House();

echo $house->get_AI($_GET['photo_AI']);

 

link: http://localhost/composeyourhouse/site/index.php?photo_AI=1.jpg

 

The thing is that $house->get_AI is empty. What am I doing wrong and how can I make effective use of OOP in this case, because I can do echo $_GET['photo_AI']; which gives the same result.

 

Thanks

Edited by dannyman1234
Link to comment
Share on other sites

You have to make another function called setAI which "sets" a value to that newly created House object. getAI should be only GETting a value, and not SETting one.

 

public function setAI($photo_AI)
{
   $this->AI=$photo_AI;
   return true;
}

 

and you getAI function shouldn't be taking $photo_AI parameter because it won't need it. Your getAI function should look like this

 

public function getAI()
{
   return $this->AI;
}

 

you may want to make a "constructor" function, which automatically runs when you create a new object.

Read about it here:

 

http://www.killersites.com/forums/topic/1732/new-oop-php-question/

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...