English 中文(简体)
工厂法没有执行?
原标题:php code is not executing?
  • 时间:2010-01-15 14:14:52
  •  标签:
  • php
  • oop
<?php
class abhi
{
    var $contents="default_abhi";

    function abhi($contents)
    {
        $this->$contents = $contents;
    }

    function get_whats_there()
    {
        return $this->$contents;
    }

}

$abhilash = new abhi("abhibutu");
echo $abhilash->get_whats_there();

?>

i 初始变数内容为缺省,也是建筑商,为什么不印刷价值,什么东西应当纠正?

见错误,

abhilash@abhilash:~$ php5 pgm2.php 

Fatal error: Cannot access empty property in /home/abhilash/pgm2.php on line 13
abhilash@abhilash:~$ 
最佳回答

你们正在以不正确的方式返回这一职能。 应当:

return $this->contents
问题回答

如果正确回顾,

$this->contents = $contents;

不适用

$this->$contents = $contents;

申请和写给提款; 申请数额不超过3美元;

而且,你是否没有在“血汗”中填写一美元,而“植被——什么? (bhilash->)

use $this->contents
i too at first had the same problem

You have a problem with $: 1. when using $this-> you do not put $ between "->" and the variable name the "$" sign, so your $this->$contents should be $this->contents. 2. in your echo youforgot the $ when calling that function from the instantiated class.

因此,你的正确守则是:

<?php
class abhi
{
    var $contents="default_abhi";

    function abhi($contents)
    {
        $this->contents = $contents;
    }

    function get_whats_there()
    {
        return $this->contents;
    }

}

$abhilash = new abhi("abhibutu");
echo $abhilash->get_whats_there();

?>




相关问题
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?

JSON with classes?

Is there a standardized way to store classes in JSON, and then converting them back into classes again from a string? For example, I might have an array of objects of type Questions. I d like to ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Passing another class amongst instances

I was wondering what is the best practice re. passing (another class) amongst two instances of the same class (lets call this Primary ). So, essentially in the constructor for the first, i can ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签