php - Call parent function inside included/required file -
i know if possible call function of parent file inside included file , how work.
example got that:
parent_file.php :
<?php if ( ! class_exists( 'parent_class' ) ) { class parent_class { public $id = 10; public static function getinstance() { if ( ! ( self::$_instance instanceof self ) ) { self::$_instance = new self(); } return self::$_instance; } public function init() { include 'child-file.php'; $child = new child_class($id); $child->action(); } public function edit($values_of_id) { return $values_of_id; } ?>
child_file.php :
<?php if ( ! class_exists( 'child_class' ) ) { class child_class { private $id; function __construct(){ $params = func_get_args(); if(!empty($params)) foreach($params[0] $key => $param) if(property_exists($this, $key)) $this->{$key} = $param; parent::__construct( array( 'id' => $this->id, ) ); } public function action() { $url = 'http://myserver.com/edit_child.php?page='. $_request['page'] .'&action=select&id='. absint($this->id) ) ); $action = '<a href='. $url .'>edit</a>' return $action; } public function select_table_row() { if ( isset( $_get['action'] ) && !empty( $_get['action'] ) ) $row = $_get['id']; $connection = new mysqli($servername, $username, $password, $dbname); // fictitious params $query = "select * mytable id = $row"; $values_of_id = mysqli_query($connection, $query); // call function of parent_file.php edit($values_of_id); } $this->select_table_row(); ?>
this fictitious example , know code couldn't work this. want aim question , make thoughts visual , maybe more comprehensible.
important cannot include parent_file.php in child_file.php because child_class access multiple files.
i'm sorry if question asked. i'm limited in buzzwords topic , couldn't find this.
you have pass parent class object child class, this:
class parentclass { private $str; public function __construct($str){ $this->str = $str; } public function getchild() { $obj = new childclass($this); $obj->callparent("send"); } public function send() { echo $this->str; } } class childclass { private $parent; public function __construct($parent) { $this->parent = $parent; } public function callparent($method) { return $this->parent->$method(); } } $obj = new parentclass("hello"); $obj->getchild(); // prints "hello"
demo: https://eval.in/403427
Comments
Post a Comment