English 中文(简体)
另一项“呼吁成员履行非目标的职能”
原标题:Another "Yet another call to a member function function() on a non-object"
  • 时间:2010-08-25 17:49:51
  •  标签:
  • php

如何简单地即时工作? 我对我创建/证实的所有类别都采取了同样的方法,但这只是给我留下这种错误的唯一方法。

 Fatal error: Call to a member function validate_fname_and_lname() on a non-object in /homepages/......../Validate.php on line 23

我的守则如下:

//Class Validate
<?php

require_once  RegExp.php ;

$myRegExp = new RegExp();

class Validate 
{
    //Sends each entry to corresponding RegExp function with appropriate regular expression
    function validate_form($un, $fname)
    {
        $err_counter = 0;

        if(!$this->myRegExp->validate_fname_and_lname($fname))
        {
            $error_identifier .=  firstName+ ;
            ++$err_counter;
        }
    }
}


//Class RegExp
<?php

class RegExp 
{
    function validate_fname_and_lname($flname)
    {
        return preg_match("/[a-zA-Z   ]{2,}/", $flname);
    }
}
最佳回答

我认为,你试图从物体范围内查阅全球<代码>myRegExp。

您应当向您的鉴定人增派一名施工人员:

public function __construct($re)
{
    $this->myRegExp = $re;
}

And then instantiate your Validator like this: $validator = new Validate($myRegExp);

你们应该宣布,我可以连任你们的职衔。


顺便提一下:我认为你应当重新思考你的设计。 如果我是的话,我会建立一个接口:

interface IValidator
{
    public function valid($input);
}

让您的专科学校实施这一接口:

class ValidateFnameAndLname implements IValidator
{
    function valid($flname)
    {
        return preg_match("/[a-zA-Z   ]{2,}/", $flname);
    }
}

建设你 类似:

class Validate 
{
    protected $myRegExp;
    public function __construct(IValidator $validator)
    {
        $this->myRegExp = $validator;
    }
    //Sends each entry to corresponding RegExp function with appropriate regular expression
    function validate_form($un, $fname)
    {
        $err_counter = 0;

        if(!$this->myRegExp->valid($fname))
        {
            $error_identifier .=  firstName+ ;
            ++$err_counter;
        }
    }
}

之后,你正在设法找到更加一致的设计。

问题回答

我猜想,这是给你造成问题的界线?

if(!$this->myRegExp->validate_fname_and_lname($fname)) 

您使用<代码>$this->myRegExp,但不是Validate类别的成员。 你已宣布米雷克特普为全球变量。





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

热门标签