回应PHP类中的值

时间:2016-07-01 03:05:11

标签: php oop static echo

我是PHP中的菜鸟,请帮助我,为什么它会回应我班级的价值观?也许在PHP工作中,类中的值是按特定方式进行的(我来自Java / C#)?

<?php

class GuestBook
{
    private static $numInstances = 0;
    private $var1 = 10;

    function __construct()
    {
        $numInstances++;
    }

    public static function getNumInstances()
    {
        return $numInstances;
    }

    public function getVar1()
    {
        return $var1;
    }
}

$instance1 = new GuestBook();
$instance2 = new GuestBook();

echo(GuestBook::getNumInstances());
echo($instance1->getVar1());

&GT;

2 个答案:

答案 0 :(得分:0)

将您的课程更改为:

class GuestBook
{
    private static $numInstances = 0;
    private $var1 = 10;

    function __construct()
    {
        static::$numInstances++;
    }

    public static function getNumInstances()
    {
        return static::$numInstances;
    }

    public function getVar1()
    {
        return $this->var1;
    }
}

将输出:

210

希望这有帮助。

答案 1 :(得分:0)

    Environment:


Request Method: GET
Request URL: http://192.168.33.10:8000/fc/episodechaptermarks/

Django Version: 1.9
Python Version: 2.7.6
Installed Applications:
('producer',
 'django.contrib.admin',
 'django.contrib.sites',
 'registration',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'storages',
 'django_extensions',
 'randomslugfield',
 'adminsortable2',
 'crispy_forms')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware')



Traceback:

File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
  149.                     response = self.process_exception_by_middleware(e, request)

File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
  147.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/usr/local/lib/python2.7/dist-packages/django/views/generic/base.py" in view
  68.             return self.dispatch(request, *args, **kwargs)

File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/mixins.py" in dispatch
  56.         return super(LoginRequiredMixin, self).dispatch(request, *args, **kwargs)

File "/usr/local/lib/python2.7/dist-packages/django/views/generic/base.py" in dispatch
  88.         return handler(request, *args, **kwargs)

File "/home/vagrant/fullcast_project/producer/views/views.py" in get
  738.             chaptermark = production.chaptermark_set.get(id=chaptermark_id).id

File "/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py" in manager_method
  122.                 return getattr(self.get_queryset(), name)(*args, **kwargs)

File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py" in get
  387.                 self.model._meta.object_name

Exception Type: DoesNotExist at /fc/episodechaptermarks/
Exception Value: ChapterMark matching query does not exist.

您需要使用类名而不是对象名

来访问静态变量

你需要使用 - &gt;对于非静态方法