La classe VARIANT

(PHP 4 >= 4.1.0, PHP 5, PHP 7)

Introduction

Le VARIANT est l'équivalent COM de zval pour PHP ; c'est une structure qui peut contenir une valeur avec un intervalle de différent types possibles. La classe VARIANT fournie par l'extension COM vous permet d'avoir plus de contrôle sur ce que PHP envoie et reçoit de COM.

Synopsis de la classe

VARIANT {
/* Méthodes */
__construct ([ mixed $value = NULL [, int $type = VT_EMPTY [, int $codepage = CP_ACP ]]] )
}

Exemples VARIANT

Exemple #1 Exemple Variant

<?php
$v 
= new VARIANT(42);
print 
"The type is " variant_get_type($v) . "<br/>";
print 
"The value is " $v "<br/>";
?>

Note:

Lors du retour d'une valeur ou récupérer une propriété variant, le variant est converti en une valeur PHP seulement quand il y a une correspondance directe entre les types qui ne produiraient pas une perte d'information. Dans tous les autres cas, la valeur est retourné en tant qu'une instance de la classe VARIANT. Vous pouvez forcer PHP de convertir ou évaluer le variant en tant que type PHP natif en utilisant les opérateurs de transtypage explicitement, ou implicitement vers une chaîne de caractères en l'affichant grâce à print. Vous pouvez utiliser la grande variété de fonctions variant pour effectuer des opérations arithmétrique sans forcer une conversion et risquer une perte de données.

Voir aussi variant_get_type().

Sommaire

add a note add a note

User Contributed Notes 4 notes

up
20
darren at dcook dot org
12 years ago
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!
up
4
richard dot quadling at carval dot co dot uk
16 years ago
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.
up
0
mark dot pearson at capita dot co dot uk
15 years ago
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!
up
-3
alain at samoun dot com
18 years ago
<?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
    
?>
To Top