- 締切済み
perlでのオブジェクト指向の書き方
以下のPHPソースをPerlで書き直したいのですが、Perlのコンストラクタがイマイチわからないので どなたかご教授ください。 可能であれば以下のソースのperlバージョンを記載して頂けると幸いで御座います。 ○呼び出し側 ・index.inc require_once('common.inc'); require_once('db.inc'); class index { var $objCommon= ''; var $objDB= ''; function index() { $this->objCommon = new common; $this->objDB = new db("mysql"); } function main() { //DB接続 $db_connect = $this->objDB->connect(); //DBから値を取得 $data = $this->objDB->sample($db_connect); } } ○呼び出される側 ・db.inc class db{ var $db_dsn_type; function db_wrap($db_type){ $this->db_dsn_type = $db_type; $this->objCommon = new common; } 各function } ・common.inc class common{ 各function } ※コンストラクタなし
- みんなの回答 (3)
- 専門家の回答
関連するQ&A
- オブジェクト指向の実際について。
初めまして、ラブ・ザ・ゲームといいます。 オブジェクト指向・・・、の理解度は単語だけ知っている、程度だと思います。 クラスを使っている掲示板があるので、さらにシンプルにして、質問させてください。 PHPは4を、想定しています。 Class main_bbs{ //ここはクラスで使う変数の宣言部分ですよね。 var $file ; var $name ; var $title ; var $message ; //ここで、コンストラクタが来ると思います。 function main_bbs(){} //Q1.コンストラクタは、何を初期化すればいいのでしょうか? //次は、データを格納する関数でしょうか。 //Q2.この部分は、クラス内で変数を使うためですよね? function FileInf($file, $name, $title, $message){ $this->file = $file ; $this->name = $name ; $this->title = $title ; $this->message = $message ; } //この掲示板では、チェックを複数に分けています。 //改行変換、危険タグの排除、等。 function Check($str){/* 処理内容 */ return $str; } //これらは、main_bbsクラス内に収まっているのですが、別のクラスに分けた方がいいと思うのですが、積極的にしない理由はありますか? //分ける理由は、その方が汎用性が高まると思うのですが。 //データの成形 function CreateDate(){ $this->name = $this->Check($this->name) ; $this->title = $this->Check($this->title) ; $this->message = $this->Check($this->message) ; } //書き込み処理 function Write(){ CreateDate()を使った後、書き込む処理。 } //書き込み処理も、別のクラスを作った方が、汎用性が高まると思うのですが、積極的にしない理由はありますか? } //呼び出し側、見ているサンプル $main_bbs = new main_bbs ; $bbs -> FileInf($file, $name, $title, $message) ; $bbs -> CreateDate() ; $bbs -> Write() ; ここまでは、参考にしている掲示板の通に書いたつもりです。 ある程度は理解できたと思います。 ・・・が、実際作成する場合、どこから着手すればいいのかが、分かりません。 小さな部分から作っていくって行けばいいのでしょうか?
- ベストアンサー
- PHP
- オブジェクト指向の上手い使い方を教えてください
以下のような、クラスにまとめた処理を一度実行してるファイルがあります。 <?php class Hoge { public function __construct() { // 設定値 $this->a = 1; $this->b = 2; $this->c = 3; } public function main() { // 設定値を使った処理 print $this->a + $this->b + $this->c; } } // インスタンス生成・実行 $hoge = new Hoge(); $hoge->main(); ?> 設定値を変更して2回処理を行いたい場合、 一番スマートな形はどれになるか教えてください。 もしくは、それ以外の方法など。 1.設定値を引数で制御し、インスタンスを2回生成する (※設定値自体を引数に渡すことは考えてないです) <?php class Hoge { public function __construct($flg) { // 設定値 if ( $flg ) { $this->a = 1; $this->b = 2; $this->c = 3; } else { $this->a = 4; $this->b = 5; $this->c = 6; } } public function main() { // 設定値を使った処理 print $this->a + $this->b + $this->c; } } // インスタンス生成・実行 $hoge = new Hoge(true); $hoge->main(); // インスタンス生成・実行 $hoge = new Hoge(false); $hoge->main(); ?> 2.インスタンスの生成・実行は1度のままにし、クラスの処理を変更する <?php class Hoge { public function __construct() { // 設定値 $this->a = 1; $this->b = 2; $this->c = 3; $this->a2 = 4; $this->b2 = 5; $this->c2 = 6; } public function main() { // 設定値を使った処理 print $this->a + $this->b + $this->c; // 設定値の変更 $this->a = $this->a2; $this->b = $this->b2; $this->c = $this->c2; // 設定値を使った処理2回目 print $this->a + $this->b + $this->c; } } // インスタンス生成・実行 $hoge = new Hoge(); $hoge->main(); ?> 3.設定値を変更するメソッドを作り、呼び出す <?php class Hoge { public function __construct() { // 設定値 $this->a = 1; $this->b = 2; $this->c = 3; } public function change() { // 設定値変更 $this->a = 4; $this->b = 5; $this->c = 6; } public function main() { // 設定値を使った処理 print $this->a + $this->b + $this->c; } } // インスタンス生成・実行 $hoge = new Hoge(); $hoge->main(); $hoge->change(); $hoge->main(); ?> 4.子クラスを作り、2回インスタンス生成・実行を行う <?php class Hoge { public function __construct() { // 設定値 $this->a = 1; $this->b = 2; $this->c = 3; } public function main() { // 設定値を使った処理 print $this->a + $this->b + $this->c; } } class Hoge2 extends Hoge{ public function __construct() { parent::__construct(); // 設定値 $this->a = 4; $this->b = 5; $this->c = 6; } } // インスタンス生成・実行 $hoge = new Hoge(); $hoge->main(); // インスタンス生成・実行 $hoge = new Hoge2(); $hoge->main(); ?> 5.設定値だけ異なるファイルを作りそれぞれ実行する ※省略 よろしくお願いします。m(__)m
- 締切済み
- PHP
- PEARを使ったフェッチのしかた。
いつもお世話になっています。 pear と phpを使ったclassを勉強しており 行き詰った点があったので教えてください。 **** class.php ************************ require_once("DB.php"); class DB_Class{ var $db_object;//DB::connectが入る var $db = "mysql"; var $db_user = "user"; var $db_host = "localhost"; var $db_name = "db_name"; var $pass = "pass"; function DB_Class(){ //コンストラクタ $db_string = $this->db."://".$this->db_user.":".$pass."@".$this->db_host."/".$this->db_name; $this->db_object = DB::connect($db_string); } } **** test.php ******************************** require_once("class.php"); $obj = new DB_Class(); $sql = "select * from TEST_TABLE"; $res = $obj->db_object->query($sql); while($row =$res->fetchRow(DB_FETCHMODE_ASSOC)){ print("<td>".$row["TEST"]."</td>"); } class.phpファイルのメンバ変数 var $db_objectにDB::connectが入っており、 test.phpでfetchRowを使ってDBの内容を 取り出したいのですが、 fetchRowメソッドが見つからないというエラー がでます。 それまでのqueryメソッドまではエラーがなく 呼び出せます。 while($row =$res->fetchRow(DB_FETCHMODE_ASSOC)) ここを $res->$obj->db_object->fetchRowや $res->$obj->$this->DB_object->fetchRowや とにかく色々な方法を試してみたのですが うまくいきませんでした。 どなたかご教授お願い致します。
- ベストアンサー
- PHP
- Ajaxのプログラムをオブジェクト指向で・・・
Ajaxのプログラムを一つのオブジェクトにまとめてみようと思ってやってみたのですが、 >エラー: プロパティ 'readyState' の値を取得できません: オブジェクトは Null または未定義です。 などと表示されて上手く行きません。 何が悪くてエラーが出るのかが理解出来ていません。 詳しい方、エラーの原因・解決方法を教えてください。お願いします。 php側 echo "test"; javascript側 //-------------------------------------------------------------------- // てすとオブジェクト //-------------------------------------------------------------------- var Test = function() {// メンバ変数の定義 this.request = ""; this.query = "test";// クエリ this.response = "";// レスポンス }; Test.prototype = {// プロトタイプの定義 /* データ送信 */ tSend : function() { if(this.query) { this.request = this.ajaxRequest(); this.request.onreadystatechange = this.tLoad; if(this.query) { this.request.open("POST", "./index.php", true); this.request.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); this.request.send(this.query); } } }, /* データ受信 */ tLoad : function() { if((this.request.readyState == 4) && (this.request.status == 200)) { alert("test"); } }, /* HTTP通信用 */ ajaxRequest : function() { var value = null; try { value = new XMLHttpRequest(); } catch(e) { try { value = new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) { try { value = new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) { return null; } } } return value; } }; //----------------------------------------------------------------------- // 実行 //----------------------------------------------------------------------- var Test = new Test();// 通信用オブジェクトの生成 Test.tSend();// データ送信
- ベストアンサー
- JavaScript
- ユーザー定義オブジェクト
現在、ユーザー定義オブジェクトについて勉強しています。 以下は本からの抜粋です。 //コンストラクタを定義する。 //thisで参照するオブジェクトの初期化法に注目 function Rectangle(w,h) { this.width=w; this.height=h; } //コンストラクタを呼び出して2つの長方形オブジェクトを生成する。 //widthとheightをコンストラクタに渡して、それぞれの新しい //オブジェクトを適切な形で初期化するやり方に注目 var rect1=new Rectangle(2,4); var rect2=new Rectangle(8.5,11); ★質問★ this.width=w; this.height=h; var rect1=new Rectangle(2,4); var rect2=new Rectangle(8.5,11); この2箇所で行っている「初期化」という作業の意味がよく分かりません。 また、ユーザー定義オブジェクトの概念についていまいちイメージ がつかめていません(特にthisとnewを使用している意図など) ご教授いただきたく、よろしくお願いします。
- ベストアンサー
- JavaScript
- オブジェクト指向 継承について
PHP初心者です。 オブジェクト指向を今勉強している途中でどうしても理解できないところがあるので教えてください。 ---------------------------------------------------------------- <? class ANIMAL{ var $first_weight; var $weight; var $soul = true; var $x = 0; var $y = 0; function ANIMAL($weight){ $this->first_weight = $this->weight = $weight; $this->old = $old; } function walk($x, $y){ if(!$this->soul){ return; } if($this->weight < $this->first_weight * 0.99){ $this->death(); return; } $this->x += $x; $this->y += $y; echo "walking... [{$x}], [{$y}]<br />"; $this->weight -= 20; } function death(){ $this->soul = false; echo "I died...<br />"; } function get_weight(){ echo "Weight... {$this->weight} g<br />"; } function get_posi(){ echo "Position... x : {$this->x}, y : {$this->y}"."<br />"; } } $animal = new ANIMAL(6000); $animal->walk(5, 2); $animal->walk(-10, 20); $animal->walk(2, 0); $animal->walk(-50, 21); $animal->walk(-11, -20); $animal->walk(20, 30); $animal->get_weight(); $animal->get_posi(); ---------------------------------------------------------------- 上記のようなプログラムがあります。 プログラムの答えの予想では、 >if($this->weight < $this->first_weight * 0.99){ 移動するごとに体重が20キロ減っていき 元体重の0.99%をきった時点5940になると $animal->walk(2, 0); $animal->walk(-50, 21); $animal->walk(-11, -20); $animal->walk(20, 30); この部分は全て I died...になると考えていました。 また、$animal->get_weight();この部分は5000gと考えていました。 return;を入れることでなにが変わるのか、戻り値という事はわかり ます。本を読んでいて本の中の簡単な例題では意味はなんとなくつかめるのですが、どんな時につかったらいいのか、また複雑になるとreturn;の存在する意味がわからなくなってしまいます。 ご助言いただければ幸いです。宜しくお願いします。
- ベストアンサー
- PHP
- オブジェクト指向のsetTimeoutの呼び出し方法
オブジェクト指向を勉強中です しかし今一しっくりきません。 setTimeoutの呼び出し部分の記述は、これでいいのでしょうか? やりたいことは、沢山の画像を一度に違ったタイミングでフェードアウト・インをすることです。 <html> <body> <img src="./img/1.gif" width="50" height="50" id="a"> <img src="./img/2.gif" width="50" height="50" id="b"> <input type="button" value="pp" onClick="img2.fin(10,50)"> <script> var img1 = new ImageEffect( 'a',100 ); img1.fout(10,30); var img2 = new ImageEffect( 'b',100 ); img2.fout(5,30); function ImageEffect( id,val1 ){ var func=[]; this.obj = document.getElementById( id ); this.opacity=(val1==undefined)? 100:val1; this.opacitySet = function(n){ this.obj.style.filter='alpha(opacity='+n+')'; this.obj.style.MozOpacity=n/100; this.obj.style.opacity=n/100; this.opacity = n; } this.fout = function(step,wtime){ func[id] = this; this.opacity-=step; if(this.opacity<1) this.opacity = 0; this.opacitySet(this.opacity); if( this.opacity ) setTimeout( function(){ func[id].fout( step, wtime);}, wtime);//ここの呼び出し } this.fin = function(step,wtime){ func[id] = this; this.opacity+=step; if(this.opacity>100) this.opacity = 100; this.opacitySet(this.opacity); if( this.opacity ) setTimeout( function(){ func[id].fin( step, wtime);}, wtime);//ここの呼び出し } }
- ベストアンサー
- JavaScript
- Webアプリ作成時のオブジェクト指向について
Webアプリ作成時のオブジェクト指向について質問させてください。 Webアプリはデータベースやテンプレートなどを使って作成することが多いと思いますが、例えばデータベースにPDO、テンプレートにSmartyを用いてHogeクラスを作る場合、 class Hoge { protected $db; protected $tpl; function __construct() { $this->db = new PDO(); $this->tpl = new Smarty(); } } のようにHogeクラスのプロパティに直接PDOとSmartyのインスタンスを持たせるのはありなのでしょうか?ただこの場合だとデータベースやテンプレートを使わずに終了した場合に無駄になってしまうような気がするが気がかりです。 かといって毎回各メソッドの中でnew PDO()やnew Smarty()をするのは面倒な気がするんですが。何かいい方法やコツがあれば教えてください。
- ベストアンサー
- PHP
- PHP4でのSmarty派生クラスの定義について
こんにちはよろしくお願いします。 PHP4でSmarty派生クラスを定義する方法を教えてください。 PHP5のサンプルコードで次のようなものがありました。 PHP4では,「private」が使えないらしいとか,「__construct」ではなくてクラス名がコンストラクタ名になるとかでうまくいかないようです。どのように修正すればよいのでしょうか? また,「__destruct」はどのようにすれば良いのでしょうか?コンストラクタ名とデストラクタ名が同じでよいのでしょうか? よろしくお願いします。 <?php require_once("DB.php"); require_once("Smarty/libs/Smarty.class.php"); class MySmarty extends Smarty { private $_db; public function __construct() { $this->Smarty(); $this->template_dir="../templates"; $this->compile_dir="../templates_c"; $this->_db=DB::connect("sqlite://localhost/samples.db"); } public function __destruct() { $this->_db->disconnect(); } public function getDb() {return $this->_db;} } ?>
- ベストアンサー
- PHP
- pearのdisconnect()が使えない。
Pearを使用してデータベースのインターフェースを抽象化したいと考ています。そこで以下のようなクラスを作りました(エラー処理は略、DB.phpは読み込んでいます) class cls_db { var $con; function connectDB() { $this -> con = DB::connect(DBの場所,true ); } function closeDB() { $this -> con -> disconnect(); } } そして、別のPHPから、 require"cls_db.php"; $db = new cls_db(); $db->connectDB(); $db->closeDB(); closeDB()を呼び出すとdisconnectが見つからないと出ます。 こういう使い方はできないのでしょうか?よろしくお願いします。
- ベストアンサー
- PHP
お礼
回答ありがとうございます。私の技術力が足りないのですぐに解析出来ませんが、頑張って解析してみます。