开发者

Declaring variable without var sign in beginning? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical 开发者_运维技巧and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

I have seen class that states var _hello_kitty = array(); why they dont use $? i tried to make it public and static and it does not work without adding $i.e. public static $_hello_kitty = array();

now when i do add $ other functions who reference it by _hello_kitty stop working.

EDIT: OOOPS was my bad i somehow removed $ from there, i looped up original file and its there. but its still referencing like $this->_tpl_vars[$tpl_var] = &$value without $ and i cannot use self::_tpl_vars[$tpl_var];

now i did use self::$_tpl_vars[$tpl_var]; but now error comes up array_merge() [function.array-merge]: Argument #1 is not an array i


You're using smarty and it looks like you're using the PHP 4 notation of class variables:

var

The PHP 5 representation of it is:

public

But you don't need to change the code because PHP is backwards compatible. Just leave it as is as a reminder that things change and for your own code you won't make use of var.

In case you will actually need to change the code because it breaks (and not you break the code because you want to change it), you will notice early enough.


That's how the syntax for PHP class properties works.

You define the property with the dollar-sign, eg

public $publicProperty;
protected $protectedProperty;
private $privatePropertyKeepOutLulz;

When referencing them from a class instance (ie object), you omit the dollar-sign, eg

$obj->publicProperty;
$this->protectedProperty;
$this->privatePropertyKeepOutLulz;

Update

Static properties are declared using the static keyword

public static $publicStaticProperty;
private static $privateStaticProperty;

You then reference them like so

// From outside the class (only applies to public properties)
ClassName::$publicStaticProperty;

// From within the class
self::$privateStaticProperty;

// From a descendant class (public or protected only)
parent::$property;


In PHP, variables are prefixed with the dollar sign, which is the programming language's sigil. This is an important concept in computing, and it's the only way PHP is able to identify variables.

From wikipedia (which never, ever lies):

In the PHP language, which was largely inspired by Perl, “$” precedes any variable name. Names not prefixed by this are considered constants or functions.

and that's it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜