php - how to call a variable from one class function to another class function -
i new php oop
i have 2 files here code
1)info.php
public $bd, $db1; class connection { function connect() { $this->db = 'hello world'; $this->db1 = 'hi' } }
2) prd.php
require_once 'info.php' class prdinfo { function productid() { echo connection::connect()->$bd; echo connection::connect()->$db1; } $prd = new prdinfo (); $prd->productid ();
how can echo var in 2nd class have tried in way not getting proper output
thanks
it should this.
info.php
class connection { // these 2 variable should declared within class. protected $db; // able access these variables diff class protected $db1; // either scope should "protected" or define getter method. public function __construct() { $this->connect(); } private function connect() { $this->db = 'hello world'; $this->db1 = 'hi'; } }
prd.php
require_once 'info.php'; // accessing connection class in static scope // not case here. class prdinfo extends connection { public function __construct() { // initialize parent class // in turn sets variables. parent::__construct(); } public function productid() { echo $this->db; echo $this->db1; } } $prd = new prdinfo (); $prd->productid ();
this basic demonstration. modify per needs. more here - http://www.php.net/manual/en/language.oop5.php
Comments
Post a Comment