Page 1 of 1

VB programmer switching to PHP... few questions

Posted: Fri Sep 01, 2006 6:49 am
by Kaervek
I have been picking up a bit of php lately and i really like it
i have one quick question for all u guru's that would really save me some time
and if i have more questions in the future i'll reuse this thread instead of spamming u guys
thanks in advance.

Q. Does php have an equivalent to VB's "with" statement?

______________________________
Sept 5: Question Asked in a new reply, scroll down

Posted: Fri Sep 01, 2006 6:54 am
by Jenk
Not exactly..

vb:

Code: Select all

With Object
    .prop1 = 1
    .prop2 = 2
End With
php:

Code: Select all

$object->prop1 = 1;
$object->prop2 = 2;
but if you are only using the values and not assigning new ones, you can:

Code: Select all

foreach ($object as $prop) {
    echo $prop;
}
or similar.

Posted: Fri Sep 01, 2006 7:02 am
by Kaervek
well in many cases i will need to assign to the properties and foreach really doesnt do the same
so i guess the answer is no, i have to reference the object at each call.

hmm i suppose if i had an object that contained another object etc i could go

Code: Select all

myWithObject = Object1->Object2->Object3;
myWithObject->myFunction();
myWithObject->Property1 = x;
etc...
how do you destroy an object?

Posted: Fri Sep 01, 2006 7:07 am
by Jenk
use function unset();

objects are also destroyed automatically when the PHP engines' reference counter for said objects reaches zero. Although for all intents and purposes, once you lose all your references, you won't have access to the object anyway..

Code: Select all

<?php

$object = new stdClass;

$object2 = $object; //objects are always passed by reference when assigning.
$object = null; // this reference to the stdClass object is no longer valid. But the object still exists in $object2.

$object3 = $object2;

unset($object2); // object is destroyed. Any references to object will not be valid anymore, ergo $object3 == null.

?>
EDIT: after a quick test, the above is only correct if you pass the object as a param in functions/methods. however the literal above is incorrect and the object will still exist, unless you assign by ref (=&)

Posted: Tue Sep 05, 2006 7:32 am
by Kaervek
Q. if i have written a user control in visual basic and compiled it into an OCX file, can i use php to embed that control into a form on a website? and if so how would i go about it?

Posted: Tue Sep 05, 2006 7:37 am
by feyd
Kaervek wrote:Q. if i have written a user control in visual basic and compiled it into an OCX file, can i use php to embed that control into a form on a website?
Yes, it's possible.
Kaervek wrote:if so how would i go about it?
Exactly as if you were writing a static HTML page. You may need/use echo(), but it's not required if you are outside of PHP already.