Global functions of PHP functions through a class

Is it possible to link all PHP functions through an object / class?

I have it on my mind and I imagine something like this:

$c = new Chainer();

$c->strtolower('StackOverFlow')->ucwords(/* the value from the first function argument */)->str_replace('St', 'B', /* the value from the first function argument */);

this should produce:

Backoverflow

Thank.

+3
source share
3 answers

Do you want to str_replace('St', 'B', ucwords(strtolower('StackOverFlow')))do

The methods you call above are functions, not methods bound to any class. Chainerwould have to implement these methods. If this is what you want to do (perhaps for another purpose, and this is just an example), your implementation Chainermight look like this:

class Chainer {
   private $string;
   public function strtolower($string) {
      $this->string = strtolower($string);
      return $this;
   }
   public function ucwords() {
      $this->string = ucwords($this->string);
      return $this;
   }
   public function str_replace($from, $to) {
      $this->string = str_replace($from, $to, $this->string);
      return $this;
   }
   public function __toString() {
      return $this->string;
   }
}

This will work in your example above, but you would call it like this:

$c = new Chainer;
echo $c->strtolower('StackOverFlow')
   ->ucwords()
   ->str_replace('St', 'B')
; //Backoverflow

, /* the value from the first function argument */ , . , , .

, , $this . , , ( $this). , .

, :

class Chainer {
   private $string;
   public function __construct($string = '') {
      $this->string = $string;
      if (!strlen($string)) {
         throw new Chainer_empty_string_exception;
      }
   }
   public function strtolower() {
      $this->string = strtolower($this->string);
      return $this;
   }
   public function ucwords() {
      $this->string = ucwords($this->string);
      return $this;
   }
   public function str_replace($from, $to) {
      $this->string = str_replace($from, $to, $this->string);
      return $this;
   }
   public function __toString() {
      return $this->string;
   }
}
class Chainer_empty_string_exception extends Exception {
   public function __construct() {
      parent::__construct("Cannot create chainer with an empty string");
   }
}

try {
   $c = new Chainer;
   echo $c->strtolower('StackOverFlow')
      ->ucwords()
      ->str_replace('St', 'B')
   ; //Backoverflow
}
catch (Chainer_empty_string_exception $cese) {
   echo $cese->getMessage();
}
+4

:

http://php.net/manual/en/language.oop5.magic.php

http://www.php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.methods

:

http://php.net/manual/en/function.call-user-func-array.php

, :

<?php
class Chainer
{
    protected $buffer = null;

    public function __call($name, $args) {
        if (method_exists($this, $name)) {
            $this->buffer = call_user_func_array(array($this, $name), $args);
        }
        elseif (function_exists($name)) {
            if ($this->buffer !== null) {
                $args[] = $this->buffer;
            }

            $this->buffer = call_user_func_array($name, $args);
        }

        return $this;
    }

    public function strpos($needle, $offset = 0) {
        return strpos($this->buffer, $needle, $offset);
    }

    public function __toString() {
        return (string)$this->buffer;
    }
}

$c = new Chainer();
echo $c->strtolower('StackOverFlow')->ucwords()->str_replace('St', 'B')->strpos('overflow'); // output: 4
+6

, Chainer :

class Chainer
{
    private $string;

    public function __construct($string = null)
    {
        $this->setString($string);
    }

    public function setString($string)
    {
        $this->string = $string;
        return $this;
    }

    public function __toString()
    {
        return $this->string;
    }

    public function strtolower($string = null)
    {
        if (null !== $string) {
            $this->setString($string);
        }
        $this->string = strtolower($this->string);
        return $this;
    }

    public function ucwords($string = null)
    {
        if (null !== $string) {
            $this->setString($string);
        }
        $this->string = ucwords($this->string);
        return $this;
    }

    public function str_replace($search, $replace, $string = null)
    {
        if (null !== $string) {
            $this->string = $string;
        }
        $this->string = str_replace($search, $replace, $this->string);
        return $this;
    }
}

, .

, __call, .

+2
source

All Articles