tags:

views:

42

answers:

1

Hi,

Sorry for the noob question.

I have the following code which works fine:

$obj = new view;

echo $obj->connect();
echo $obj->content_two(); 

Basically what I need to do is get a variable called $variable from inside the function content_two(), How would I call $variable from content_two() ?

Thanks in advance.

+3  A: 

With a member property.

<?php
  class View {
    protected $foo;
    function connect() {
      $this->foo = 42;
      return "Hello from connect";
    }
    function connectTwo() {
      return "Hello from connectTwo. foo = " . $this->foo;
    }
  }

Note that the conventions dictates that classes are named in CapitalCamelCase, whereas methods (functions on objects) and properties (variables on objects) are named in lowerCamelCase. You don't have to follow the convention, but it's probably a good idea to do so.

troelskn