views:

204

answers:

4

Bear with me cos I'm new to oop! Is it possible to assign the result of a function to an attribute?
This probably doesn't make sense so here's what I think the code might look like!

Class b extends a 
{ 
    public $conn= $this->connect();

    public function operation() { ...}

}

connect() is a function from class a that connects to a database and returns $result if successful.

+8  A: 

Yes that is possible but you need to do that in a method like the constructor:

Class b extends a { 
    public $conn;
    public function __construct() {
        $this->conn = $this->connect();
    }
    public function operation() { /* ... */ }
}
Gumbo
Please see my answer. This is dangerous unless your class cannot be derived from or the `connect` method cannot be overriden.
jeyoung
A: 

You need to put the call to connect within a method, and then call the method once you've instantiated the class to initialize the connection - or use the constructor method to do so automatically.

Amber
A: 

Generally, this is bad form, as you cannot be certain what implementation of the connect() method will be called. Is it the one from the current class or the one from the superclass? In fact, I would be surprised if PHP even allowed this.

Initialisation should be done in the constructor, but even then it depends on what the initialisation involves. If it requires calling another method, then the same issue as above exists: which version of the method to call?

However, for the scenario you describe, I would neither initialise the member variable at declaration, nor from within a constructor. Instead, I would pass $conn to the constructor. This is the basis of dependency-injection.

jeyoung
PHP allows that kind of functionality. In fact, when *B* overrides `connect`, `$conn` will be the return value of *B*’s `connect` method, and if not, then it will be the one of *A*’s `connect` method.
Gumbo
I have not done any PHP since 2004, so I stand corrected. However, I still maintain that it is bad form to call a non-final method from a constructor, as you cannot guarantee that a derived class will not break your constructor and make your class invalid.The original author seems to want to use this as a shortcut (using method inheritance to avoid repeating code), which is bad. I would recommend that he passes the connection to the construtor (dependency injection) or does the initialisation of the variable in the constructor without a call to a non-final method.
jeyoung
+1  A: 

i usually do something like this

$link = mysql_connect(.....,......,......);

Class b extends a { 
    public $conn;
    public function __construct($link) {
        $this->conn = $link;
    }
    public function operation() { /* ... */ }
}
$a = new b($link);

then if your connection details change you change them in one place and if you need to connect to a different server you can and pass in the other link variable.

Another tip for oop database integration is always have a DBable base class to do your basic save() load() find() count() etc, much quicker to work with db savable objects

BaseDBClass
   A
      B
      C
      D
   X
      Y
      Z
Question Mark