クラスを自動ロードするコードを書いていますが、実装/設計タイプの脆弱性が原因であると思われる問題に遭遇しました。私がやりたいことは、オブジェクト(外部)のデフォルトパラメータをカウントすることです。コンストラクターに渡された引数の数を数えることはできますが、オブジェクト コンストラクター内で確認する必要があり、そのメソッドは役に立ちません。
コード例:
// This is simple
function test($arg1,$arg2,$arg3) {return func_num_args();}
// How can I count like this?
class load
{
public function __construct($id="",$path="") {}
}
$l = new load();
// How to count object default parameters count(object($l)), I need answer to be 2`
この方法を使用する必要がある私のコード:
[ファイル: global_cfg.php]
<?php
// File: global_cfg.php
define(ROOT, __DIR__); // Root directory
define(DEBUG, true); // Set debugging state ON or OFF
define(MODE, "producer"); // If debug mode is ON: producer, publisher, tester
/*
* PATH CONFIGURATIONS:
*/
define(DS, "/");
define(LIB, "library");
/*
* SIGN AUTOLOAD CLASSES:
* Setting class sign to true value, the autoloader will create automatically
* an instance of the class lowercase type.
*/
$signClasses = Array
(
"Ralor" => false,
"NaNExist" => true,
"Message" => array(MODE),
"Debug" => DEBUG,
"Resource" => true,
"View" => true
);
[ファイル: autoload_classes.php]
<?php
// File: autoload_classes.php
require_once("global_cfg.php");
print "<b>Loaded classes:</b> <br>";
function __autoloadClasses($list, $suffix="class", $extension="php")
{
$path="";
foreach($list as $fileName => $classInstance)
{
$path = ROOT.DS.LIB.DS.$fileName.".".$suffix.".".$extension;
if(!file_exists($path))
{
print "Signed class ".$fileName." does not exist!<br>";
continue;
}
require_once($path);
print $path;
if($classInstance)
{
$GLOBALS[strtolower($fileName)] = new $fileName();
// ??? todo: counting default object parameters
$count = count(get_object_vars($GLOBALS[strtolower($fileName)]));
if(is_array($classInstance))
{
if($count<count($classInstance))
{
print "Arguments passed to object exceeds the limit";
}
else if($count>count($classInstance))
{
print "Insuficient arguments passed to the object!";
}
else
{
// todo: create object and pass parameters
$GLOBALS[strtolower($fileName)] = new $fileName(/*$arg1 .. $argn*/);
}
}
print $count." -> Class was instantiated!<br>";
continue;
}
print "<br>";
}
}__autoloadClasses($signClasses);
この問題の後、ブートストラップを終了できます。