If you are frustrated that print_r($obj) (where $obj is something returned from a call to a function on a COM object) does not return anything helpful, and that variant_get_type($obj) just returns a number, the function you are actually after is:
com_print_typeinfo($obj);
It lists all functions, variables, their types in a human-readable (well, programmer-readable) format. Lovely!
VARIANT
(No version information available, might be only in CVS)
VARIANT — VARIANT クラス
$vVar = new VARIANT($var)
説明
VARIANT は、PHP の zval と同等の COM の値です。構造体形式になっており、 異なる型の値を含めることが可能です。COM 拡張モジュールが提供する VARIANT クラスによって、PHP と COM の間のデータの受け渡しを制御できるように なります。
メソッド
VARIANT クラスのコンストラクタ。パラメータは以下のとおりです。
- value
- 初期値。省略したり NULL を設定した場合には、VT_EMPTY オブジェクトが作成されます。
- type
- VARIANT オブジェクトの content 型を指定します。使用可能な値は、 VT_XXX 定義済み定数 のうちのひとつです。 PHP 5 より前のバージョンでは、type に VT_BYREF を加えることで PHP から variant オブジェクトに参照渡しをさせることが可能でした。PHP 5 では、 この方法はサポートされていません。そのかわりに、PHP 5 では パラメータが参照渡しされた場合にそれを自動的に認識します。 VARIANT オブジェクトとして渡す必要さえありません。 VARIANT 型についてのその他の情報については、MSDN ライブラリを参照ください。
- codepage
- 文字列を unicode に変換する際に使用するコードページを指定します。 詳細な情報については、 COM クラスの同名のパラメータを参照ください。
PHP 5 より前のバージョンでは、VARIANT クラスのインスタンスには 多くの(ドキュメント化されていない)仮想プロパティが存在します。 これらのプロパティは PHP 5 ですべて削除され、かわりにより自然な構文で アクセスできるようになっています。これらの違いについて、以下の例で 説明します。
例1 PHP 4.x 形式 での Variant の例
<?php
$v = new VARIANT(42);
print "The type is " . $v->type . "<br/>";
print "The value is " . $v->value . "<br/>";
?>
例2 PHP 5 形式での Variant の例
<?php
$v = new VARIANT(42);
print "The type is " . variant_get_type($v) . "<br/>";
print "The value is " . $v . "<br/>";
?>
このように変更された理由は、内部的には、COM 拡張モジュールが VARIANT、COM および DOTNET クラスを同一のものとして扱い、これらの クラスのプロパティやメンバへのアクセスは(何のインターフェースも 経由せず)直接 COM とやり取りを行うようにするという設計思想によるものです。 新しい構文は、より自然でより手軽です。また、削除された仮想プロパティの ほとんどは、PHP のコンテキストにおいては何の意味も持たないものでした。
注意: PHP 5 では、VARIANT を扱うためのよりシンプルな手段をとっています。 値を返したり variant プロパティを取得したりする際に variant が PHP の値に変換されるのは、情報を失うことなく型変換ができる型が 存在する場合に限られます。それ以外の場合は、結果は VARIANT クラスの インスタンスとして返されます。明示的にキャスト演算子を指定することで variant を PHP のネイティブ型として扱うことが可能です。また、 print() を使用すると、暗黙のうちに値が文字列に 変換されます。variant に対する計算のためのさまざまな関数が用意されており、 型変換でデータを失うリスクを犯さなくてもそのままの形式で計算を 行うことが可能です。
variant_get_type() も参照ください。
VARIANT
17-Jul-2007 05:59
29-Oct-2003 11:51
Running PHP 4.3.2 on Windows 2000 I had to use the following expression to create an empty Variant:
<?php
$empty = new Variant(null);
print $empty->type // ==> 1
?>
NOT
<?php
$empty = new Variant();
print $empty->type // ==> 0
?>
The two expressions return different Variant type values!
26-Feb-2003 05:50
With thanks to Harald Radi and Wez Furlong.
Some VBA functions have optional parameters. Sometimes the parameters you want to pass are not consecutive.
e.g.
GoTo What:=wdGoToBookmark, Name="BookMarkName"
GoTo(wdGoToBookmark,,,"BookMarkName)
In PHP, the "blank" parameters need to be empty.
Which is ...
<?php
// Some servers may have an auto timeout, so take as long as you want.
set_time_limit(0);
// Show all errors, warnings and notices whilst developing.
error_reporting(E_ALL);
// Used as a placeholder in certain COM functions where no parameter is required.
$empty = new VARIANT();
// Load the appropriate type library.
com_load_typelib('Word.Application');
// Create an object to use.
$word = new COM('word.application') or die('Unable to load Word');
print "Loaded Word, version {$word->Version}\n";
// Open a new document with bookmarks of YourName and YourAge.
$word->Documents->Open('C:/Unfilled.DOC');
// Fill in the information from the form.
$word->Selection->GoTo(wdGoToBookmark,$empty,$empty,'YourName'); // Note use of wdGoToBookmark, from the typelibrary and the use of $empty.
$word->Selection->TypeText($_GET['YourName']);
$word->Selection->GoTo(wdGoToBookmark,$empty,$empty,'YourAge');
$word->Selection->TypeText($_GET['YourAge']);
// Save it, close word and finish.
$word->Documents[1]->SaveAs("C:/{$_GET['YourName']}.doc");
$word->Quit();
$word->Release();
$word = null;
print "Word closed.\n";
?>
The example document is ...
Hello [Bookmark of YourName], you are [Bookmark of YourAge] years old.
and it would be called ...
word.php?YourName=Richard%20Quadling&YourAge=35
Regards,
Richard.
02-Sep-2001 01:37
<?php
# I think that we need some examples of this thing:
# Lets define a real variant:
$varREAL= new Variant("9.34 is a real number",VT_R8);
print "Value:". $varREAL->value; # Will print 9.34
# Now an integer
$varINT= new Variant("9.34 Printed as an integer",VT_INT);
print "Value:". $varINT->value; # Will print 9
# Now a string
$varSTR= new Variant("9.34 Printed as a string",VT_BSTR);
print "Value:". $varSTR->value; # Will 9.34 Printed as a string
?>
