简体   繁体   中英

Simple OOP PHP Question

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM