Jump to content

[Beginner Question] accesing outer variables inside a class


elnaz1010

Recommended Posts

hi

im very new to php, and i want to access a variable that is outside a class, but i cant :(

here is the code:

<?php
$name="Test";
class Person{
	var
	   $name="Amin";
	function getName() {
	   return $name;
	}
}
$obj=new Person();
echo $obj->getName();
?>

if i set "return $this" to "$this->name" it will give me "Amin".

but what i should do if i want the "Test" value?

 

thanks

Link to comment
Share on other sites

Give your getName() function a parameter like so:

<?php
function getName($name){
       //do stuff here
}
?>

 

That's how you would pass an outside variable into a function. (There are other ways, as well).

 

And that way, you can do this:

 

<?php
      $person_name = 'Mike';
      $obj->getName($person_name);
?>

Link to comment
Share on other sites

You need to pass the value into the object first. eg;

 

<?php

class Person {
  
  private $_name;
  
  public function setName($name)
  {
    $this->_name = $name;
  }

  public function getName()
  {
    return $this->_name;
  }

}

$name = 'Test';

$p = new Person;
$p->setName($name);
echo $p->getName();

Link to comment
Share on other sites

thank you Goldeneye.

but it seems i couldnt express my mean (sorry about that).

i was trying to access the outer $name (which has a "Test" value) in the class. so the getName function will return "Test" value.

but anyway i found the solution, so i say here, maybe someone come up with my problem.

i should use "global" keyword in my class function, so the code will be like:

<?php
$name="Test";
class Person{
	var
		$name="Amin";
	function getName() {
		global $name;
		return $name;
	}
}
$obj=new Person();
echo $obj->getName();
?>

 

 

thanks again

 

regards

 

Edit:

also thanks to "thorpe" who replied me while i was typing my second post :)

Link to comment
Share on other sites

excuse me, but im a bit confused.

if im not supposed to use globals, then what i should do in a situation like this?

i like to return "Test" with my getName function.

 

i know my questions is very newbie, please bear with me :)

 

thanks

 

Simply pass the outer variable to the object via a function:

 

class Person
{
   private $name;

   public function setName($name)
   {
      $this->name = $name;
   }

   public function getName()
   {
      return $this->name;
   }
}

$test = "test"
$person = new Person();
$person->setName($test);
echo $person->getName();

Link to comment
Share on other sites

excuse me, but im a bit confused.

if im not supposed to use globals, then what i should do in a situation like this?

i like to return "Test" with my getName function.

 

i know my questions is very newbie, please bear with me :)

 

thanks

 

Simply pass the outer variable to the object via a function:

 

class Person
{
   private $name;

   public function setName($name)
   {
      $this->name = $name;
   }

   public function getName()
   {
      return $this->name;
   }
}

$test = "test"
$person = new Person();
$person->setName($test);
echo $person->getName();

 

Well said. Did you see this reply?

Link to comment
Share on other sites

excuse me, but im a bit confused.

if im not supposed to use globals, then what i should do in a situation like this?

i like to return "Test" with my getName function.

 

i know my questions is very newbie, please bear with me :)

 

thanks

 

Simply pass the outer variable to the object via a function:

 

class Person
{
   private $name;

   public function setName($name)
   {
      $this->name = $name;
   }

   public function getName()
   {
      return $this->name;
   }
}

$test = "test"
$person = new Person();
$person->setName($test);
echo $person->getName();

 

Well said. Did you see this reply?

 

LMAO, nope.  That will teach me to jump directly to the last message or two in a thread.

Link to comment
Share on other sites

Not to beat this topic to death, but I'm going to request to elnaz that you forget the PHP global keyword exists.

 

If you ever, ever find yourself wanting to type global in your PHP code, then you need to come back here and ask us what the better way is.

 

Globals are worse than murdering kittens and puppies and Hell won't even admit people who use them.

Link to comment
Share on other sites

Not to beat this topic to death, but I'm going to request to elnaz that you forget the PHP global keyword exists.

 

If you ever, ever find yourself wanting to type global in your PHP code, then you need to come back here and ask us what the better way is.

 

Globals are worse than murdering kittens and puppies and Hell won't even admit people who use them.

hehe, you remind me of my "basic" language teacher, who was talking like you about using "goto" statement :D

ok mate, i will remember that

thanks all for helping me

 

have a good day

Link to comment
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.