|
-
Oct 22nd, 2008, 06:40 AM
#1
Thread Starter
Lively Member
___________________
________________________________
Last edited by Y.P.Y; Apr 28th, 2012 at 05:12 PM.
-
Oct 22nd, 2008, 08:24 AM
#2
Re: public $X V.S. var $X
scope. var is local scope. public is, well, public.
-tg
-
Oct 22nd, 2008, 08:32 AM
#3
Re: public $X V.S. var $X
i have never seen 'var' used in PHP. Can you show me where you have seen it?
i know var is used in Javascript to define a string
My usual boring signature: Something
-
Oct 22nd, 2008, 09:34 AM
#4
Thread Starter
Lively Member
Re: public $X V.S. var $X
MyBB database class(MySQL). and...
-
Oct 22nd, 2008, 12:00 PM
#5
Re: public $X V.S. var $X
@dclamp - well, how else do you define a local variable in PHP? That's the way Iv'e always done it....
-tg
-
Oct 22nd, 2008, 12:51 PM
#6
Re: public $X V.S. var $X
Because PHP is typeless, all variables are defined when they are first used; variables are given the type of the value they are assigned. Therefore in order to declare an integer you would use:
There are instances where it is useful to declare the presence of a variable which may or may not be used. The var keyword is used to declare class variables only and was introduced in PHP 4.
PHP Code:
class Foo { var $myProperty;
function Foo() { $this->myProperty = 'hello'; } }
Before a declared class variable is used it is assigned a null value. In PHP 5 var was superseded by the class visibility modifiers (public / private and protected). var can still be used in PHP 5 but it is generally considered better practice to use either public, private or protected. Var will likely also be removed in PHP 6.
PHP Code:
class Foo { private $myProperty;
public function __construct() { $this->myProperty = 'hello'; } }
The visibility modifiers can also be applied to class methods in PHP 5 too. PHP is becoming more of a typed language since the introduction of the PHP 5 object model which also introduced type hinting to force function arguments to be of a particular object type.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|