0

Very new to classes in PHP, How can I access the variable from within the class inside a function?

 //how can I echo the value of the title variable outside the class?
 echo $title; 
 class myClass {

  function form() {
      echo '<input type="text" value="'.$title.'" />';
   }

 }   

Thanks for the help

zadubz
  • 1,125
  • 2
  • 17
  • 31
  • do you want to access a variable that is outside the class while you are in the class, or should the variable be inside the class too ? – Frederik.L Mar 02 '13 at 23:42
  • I need to access the value of the variable inside the class called 'tile', I need to get that value out of the class into a variable – zadubz Mar 02 '13 at 23:46
  • what you could do then is to add a getTile() method, which simply does: return $this->tile; Then you can call getTile from wherever you need the tile value. – maraspin Mar 03 '13 at 00:00
  • The method idea sounds good how can I do that? – zadubz Mar 03 '13 at 00:22

3 Answers3

4

First of all, your variable $title doesn't seem to be defined anywhere in the scope of the code you have shared.


Nonetheless, I can share four ways to handle this.

1. By having title as a member of the class myClass, which would be specific for every instance of that class

class myClass {
    public $title = '';
  function form() {
      echo '<input type="text" value="'.$this->title.'" />';
   }

}

$myClass = new myClass();
$myClass->title = 'My title';

2. By using a constant within the class, which will be the same on all myClass class instances

class myClass {
     const title = 'My title';
  function form() {
      echo '<input type="text" value="'.myClass::title.'" />';
   }

}

3. Passing the variable to the function you will be calling

class myClass {
  function form($title) {
      echo '<input type="text" value="'.$title.'" />';
   }

}

$myClass = new myClass();
$myClass->form('My title');

4. Using a global variable like you are trying to do.

Please, use global variables with caution. Don't use them if you don't need to as explained here and here.

$title = 'My title';
class myClass {
    function form() {
        global $title; // <-- declare here that we will use the global variable $title
        echo '<input type="text" value="'.$title.'" />';
    }
}
Community
  • 1
  • 1
Shef
  • 41,793
  • 15
  • 74
  • 88
  • please let's not suggest people to use globals. They're always source for code mess ;-) – maraspin Mar 02 '13 at 23:52
  • 1
    yeah, why not passing an argument in 1) instead of using global? – Marko D Mar 02 '13 at 23:57
  • 1
    @MarkoD Left overs... fixed now, thanks! – Shef Mar 03 '13 at 00:04
  • @maraspin I would suggest the OP to be cautions with global variables, not to abuse them, but suggesting to not use them at all is far away from my bag of tricks. – Shef Mar 03 '13 at 00:06
  • 1
    i'd just suggest that you put the solution using global at the end of your list, as the least good (or no good at all) – Marko D Mar 03 '13 at 00:07
  • what are benefits of global vars? IMHO global vars imply no OO. How can you guarrantee abstraction, encapsulation? My 2cc – maraspin Mar 03 '13 at 00:08
  • good point, @MarkoD1. As I said above, IMHO it makes little sense to use globals with OO. You lose much of OO benefits. BTW In a context where Demeter law is generally considered a good practice, it should turn out obvious that globals are something to avoid. Again, just my 2cc. – maraspin Mar 03 '13 at 00:10
  • well, you have my vote up with the latest edit – Marko D Mar 03 '13 at 00:15
  • Good edit! But #3 should still be #1. In fact #1 violates encapsulation, which is fundamental in OO. http://en.wikipedia.org/wiki/Encapsulation_%28object-oriented_programming%29 ;-) ...and please, remove #4. Which is dangerous to newcomers. To say the least! ;-) – maraspin Mar 03 '13 at 00:19
  • @maraspin In small projects some practical use for globals is `$db`, `$language`, etc... Carrying around the database connection as an argument or language will be a mess. And a constant is not always the best choice for some data types. So, we have to live with it! – Shef Mar 03 '13 at 00:23
  • 1
    @maraspin Encapsulation could be given back by declaring the variable as `private` and initializing it on instantiation, but that would give the OP more than s/he can chew. – Shef Mar 03 '13 at 00:26
  • I see your point. But an experienced programmer only can tell when a global could be a good idea. I think as a general rule it's dangerous to offer such an alternative to beginners. You know it'll be abused. ;-) – maraspin Mar 03 '13 at 00:31
  • @Shef IMHO little benefit in using OO construct without grasping the concepts behind it. It's not using the word class that projects go OO. Got your point, though. And not my intention to argue everything. Just to put a (few) word(s) of caution on risky practices. My 2cc. Thanks for sharing your point of view. – maraspin Mar 03 '13 at 00:34
  • 2
    @maraspin There are 1 billion great suggestions we could give to beginners. For example in this case, to escape the given input to protect their app against XSS and so on and so forth. Practice will teach them the best ways of doing it! Whoever hungers to learn, will learn the best practices. This is just the beginnings. There is a long way ahead of them to learn all of the bests. – Shef Mar 03 '13 at 00:34
4

What you could do, so to be able to use an outside variable, without having to rely on globals (a bad practice) is to pass the variable from outside. Like this:

  class myClass {

    function form($title) {
      echo '<input type="text" value="'.$title.'" />';
     }

 }   

and then you can call your function like this:

$class = new myClass();
$class->form('This is going to be the title');

Globals are bad, because you create couplings and you lose track of what happens around your application. By modifying a global variable in one place, you risk of hurting code somewhere else. Therefore it's always better to rely on encapsulation, and explicitly "inject" stuff you need.

maraspin
  • 2,295
  • 19
  • 15
0
 class myClass {
 public $title;     
  function form() {
      echo '<input type="text" value="'.$this->title.'" />';
   }

 }  

You said, how to access within a class, but in your example, your property/variable was not inside the class. Just sayin..