English 中文(简体)
C-style Variable initialization in PHP
原标题:

Is there such a thing as local, private, static and public variables in PHP? If so, can you give samples of each and how their scope is demonstrated inside and outside the class and inside functions?

最佳回答

I don t know about C++ but there s how PHP works about:

For Function scopes:

<?php

  $b = 6;

  function testFunc($a){
    echo $a. - .$b;
  }

  function testFunc2($a){
    global $b;
    echo $a. - .$b;
  }

  testFunc(3);
  testFunc2(3);

?>

The output is

3-
3-6

Code inside functions can only be accessed variables outside functions using the global keyword. See http://php.net/manual/en/language.variables.scope.php

As for classes:

<?php

class ExampleClass{

  private $private_var = 40;
  public $public_var = 20;
  public static $static_var = 50;

  private function classFuncOne(){
    echo $this->private_var. - .$this->public_var; // outputs class variables
  }

  public function classFuncTwo(){
    $this->classFuncOne();
    echo $private_var. - .$public_var; // outputs local variable, not class variable
  }

}

$myobj = new ExampleClass();

$myobj->classFuncTwo();
echo ExampleClass::$static_var;
$myobj->classFuncOne(); // fatal error occurs because method is private

?>

Output would be:

40-20
-
50
Fatal error: Call to private method ExampleClass::classFuncOne() from context in C:xampphtdocsscope.php on line 22

One note to take: PHP does not have variable initialization, although variables are said to be set or not set. When a variable is set, it has been assigned with a value. You can use the unset to remove the variable and its value. A not set variable is equivalent to false, and if you use it with all errors output, you will see a E_NOTICE error.

问题回答

In PHP there are static, local, private, public, and protected class variables.

However, in the PHP OOP implementation things are a little different: the manual will help you:

The visibility of a property or method can be defined by prefixing the declaration with the keywords public, protected or private.

Furthermore, have a look at the static keyword documentation.

You ll be able to read more about normal variables and their scope here: http://php.net/manual/en/language.variables.scope.php:

For the most part all PHP variables only have a single scope.

I hope the links will be able to explain it to you better than I did ;-)

yes, PHP 5 incldude static variables and visibility in class.

class MyClass
{
    public static $my_static =  foo ;
    public $public =  Public ;
    protected $protected =  Protected ;
    private $private =  Private ;

    public function staticValue() {
       return self::$my_static;
    }
    function printHello()
    {
        echo $this->public;
        echo $this->protected;
        echo $this->private;
    }
}

$obj = new MyClass();
echo $obj->public; // Works
echo $obj->protected; // Fatal Error
echo $obj->private; // Fatal Error
$obj->printHello(); // Shows Public, Protected and Private




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签