English 中文(简体)
1. 具有标的类别领域
原标题:initializing class field with an object
  • 时间:2012-05-05 07:28:34
  •  标签:
  • php

I m new to php so the question isump. 审视以下法典

class A {
    private $obj;

    public function A() {
        $this->obj = new Obj();
        //$this->obj->fff() works here;
        //still var_dump($this->obj) prints NULL here
    }

    public f() {
        //$this->obj is NULL here!!!
        //$this->obj->ff() throws an error
    }
}

UPD in f() I get Fatal error: Call to a member function ff() on a non-object in ...

如何在<条码>上填写。

advance!

最佳回答

如下:

public function __construct() {
     $this->obj = new Obj();
}

我认为问题在于你错失了<条码>功能<>。 我实际上不是<代码>PHP guy,但我想public function A(也将发挥作用,但-ruct更可取。

Edit: 现在,你添加了功能。 如果我的回答能够帮助你的话,我并不真心。

问题回答

页: 1 当你打电话<代码>$a = 新的A; 实际A(,/A 类别。 使用<代码>-(......)。

在我的服务器上,这项工程课以罚款:

<?php 

class A {

    private $obj;

    public function A() {

        $this->obj = new stdClass();

    }

    public function f() {

        var_dump($this->obj);

    }

}

$aa = new A;

$aa->f(); // Prints `object(stdClass)#2 (0) { }`

?>

象你一样,对你的<代码”有问题。 Obj category. 为什么不把<条码>功能列入<条码>的公开 f(>{?





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签