Access variable from function inside seperate class? (PHP)

  • fibonacci_bs
  • Born
  • Born
  • fibonacci_bs
  • Posts: 1

Post 3+ Months Ago

Is it possible for to access a variable that has been declared in a function inside of another class? Here's an example of how I mean..

db.php
PHP Code: [ Select ]
class Connection
{
...
function __construct() {
...
$this->mysql = new mysqli(params);
...
}
}
 
  1. class Connection
  2. {
  3. ...
  4. function __construct() {
  5. ...
  6. $this->mysql = new mysqli(params);
  7. ...
  8. }
  9. }
  10.  


other.php
PHP Code: [ Select ]
require('db.php');
 
class Other extends Connection
{
...
function SomeOther() {
...
$query  = 'SOME QUERY';
$result = parent::$this->mysqli->query($query); # Not sure if this works, haven't tested it, but Geany says it's ok
...
}
}
 
  1. require('db.php');
  2.  
  3. class Other extends Connection
  4. {
  5. ...
  6. function SomeOther() {
  7. ...
  8. $query  = 'SOME QUERY';
  9. $result = parent::$this->mysqli->query($query); # Not sure if this works, haven't tested it, but Geany says it's ok
  10. ...
  11. }
  12. }
  13.  


I know that isn't how to do it (I am probably far off), but I was wondering if it's possible?
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

Simple answer, yes it's possible.

Complicated answer, the visibility of the property must not be "private" and since you're accessing it through parent:: the constructor for Other will need to explicitly call parent::__construct if you define a __construct in Other.

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 104 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.