note 114367 added to class.stackable

From: Date: Thu, 13 Feb 2014 05:47:06 +0000
Subject: note 114367 added to class.stackable
Groups: php.notes 
Request: Send a blank email to php-notes+get-198486@lists.php.net to get a copy of this message
How to give worker a class instance which has a resoure, such as database access class? I found a way of that: you just need to modify you class to extends Stackable, then add a empty run method, then modify all private methods to public method. Example: <?php //old one class db { private $conn=NULL; public function db($user,$pwd='',$db_name,$host='localhost',$_charset='utf8',$connectImediatly = false){ $this->doConnect(); } private function doConnect($flag=false) { $this->conn = @mysql_connect($host,$user,$pwd,1) or die('A'.mysql_error()); $this->doselect($db_name,$this->charset); } } ===> <?php //new one , $db=new $db(...); $worker = new myworker($db); //$db will work well in your worker class db extends Stackable{ private $conn=NULL; public function db($user,$pwd='',$db_name,$host='localhost',$_charset='utf8',$connectImediatly = false){ $this->doConnect(); } public function doConnect($flag=false) { $this->conn = @mysql_connect($host,$user,$pwd,1) or die('A'.mysql_error()); $this->doselect($db_name,$this->charset); } public function run(){ } } ---- Server IP: 92.48.119.85 Probable Submitter: 182.139.39.2 ---- Manual Page -- http://php.net/manual/en/class.stackable.php Edit -- https://master.php.net/note/edit/114367 Del: integrated -- https://master.php.net/note/delete/114367/integrated Del: useless -- https://master.php.net/note/delete/114367/useless Del: bad code -- https://master.php.net/note/delete/114367/bad+code Del: spam -- https://master.php.net/note/delete/114367/spam Del: non-english -- https://master.php.net/note/delete/114367/non-english Del: in docs -- https://master.php.net/note/delete/114367/in+docs Del: other reasons-- https://master.php.net/note/delete/114367 Reject -- https://master.php.net/note/reject/114367 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#198486) next »