2016-10-07 39 views
0

私はこのPHPコードを持っており、class formular_validiationからfunction firstnameLength()に電話したいと思っています。クラス内の関数から関数を呼び出す

class formular_validiation 
{ 
    private static $minLength = 2; 
    private static $maxLength = 250; 
    public static function firstname() { 

     function firstnameLength($firstnameLength){ 
      if ($firstnameLength < self::$minLength){ 

      } 
      elseif ($firstnameLength > self::$maxLength) { 

      } 
     } 

     function firstnameNoSpace($firstnameNoSpace) { 
      preg_replace(" ", "", $firstnameNoSpace); 
     } 

    } 
} 

私のような何かについてthougth:

formular_validiation::firstname()::firstnamelength() 

が、これは間違っています。

+0

使用 'ます$ this-> FUNCTION_NAMEを'同じクラスのコール関数のために – abhayendra

+0

これは機能しません。なぜなら関数が関数の中にあるからです。 – Blueblazer172

答えて

1

method chainingあなたが探していると呼ばれていますが、静的に最初のメソッドを呼び出したい場合は、あなたのような何かを行う必要があります。

class FormularValidation 
{ 
    private $minLength = 2; 
    private $maxLength = 250; 
    private $firstname; 

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

    public static function firstname($firstname) { 
     return new self($firstname); 
    } 

    public function firstnameLength() 
    { 
     $firstnameLength = strlen($this->firstname); 

     if ($firstnameLength < $this->minLength){ 
      return 'something'; 
     } 
     elseif ($firstnameLength > $this->maxLength) { 
      return 'something else'; 
     } 
    } 

    public function firstnameNoSpace() 
    { 
     return preg_replace(" ", "", $this->firstname); 
    } 
} 

使用法:

$firstnameLength = FormularValidation::firstname('Mihai')->firstnameLength(); 
+0

それを修正するために時間をとってくれてありがとうございます:)すごくうまくいきます – Blueblazer172

関連する問題