2016-10-07 1 views
0

Ich habe diesen PHP-Code und ich möchte die function firstnameLength() von der class formular_validiation aufrufen.Aufruf einer Funktion von einer Funktion in einer Klasse

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); 
     } 

    } 
} 

ich thougth über so etwas wie:

formular_validiation::firstname()::firstnamelength() 

aber das ist falsch.

+0

Gebrauch '$ this-> function_name 'für Anruf Funktion in der gleichen Klasse – abhayendra

+0

wird dies nicht funktionieren, weil die Funktion in einer Funktion ist – Blueblazer172

Antwort

1

Was Sie suchen method chaining genannt wird, aber wenn Sie die erste Methode aufgerufen werden soll statisch sollten Sie so etwas wie:

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); 
    } 
} 

Verbrauch:

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

danke für die Zeit nehmen, um es zu korrigieren :) funktioniert awasome – Blueblazer172

Verwandte Themen