• Login
  • Register
  • Zoek
This Thread
  • Everywhere
  • This Thread
  • This Forum
  • Articles
  • Pages
  • Forum
  • Filebase Entry
  • More Options

ICTscripters

Dé plek voor IT

Dé plek voor IT

Login

Geavanceerde opties
  1. Home
  2. Forum
    1. Alle berichten
    2. Recente activiteiten
  3. ICT Nieuws
  4. Blog
  5. Marktplaats
    1. Werk
    2. Advertenties
    3. Domeinnamen
    4. Websites
    5. Design & lay-outs
    6. Scripts
    7. Overige
  6. Design
  7. Leden
    1. Actieve bezoekers
    2. Team
    3. Leden zoeken
  8. Downloads
  9. Goedkope domeinnamen
  1. Home
  2. Forum
    1. Alle berichten
    2. Recente activiteiten
  3. ICT Nieuws
  4. Blog
  5. Marktplaats
    1. Werk
    2. Advertenties
    3. Domeinnamen
    4. Websites
    5. Design & lay-outs
    6. Scripts
    7. Overige
  6. Design
  7. Leden
    1. Actieve bezoekers
    2. Team
    3. Leden zoeken
  8. Downloads
  9. Goedkope domeinnamen
  1. Home
  2. Forum
    1. Alle berichten
    2. Recente activiteiten
  3. ICT Nieuws
  4. Blog
  5. Marktplaats
    1. Werk
    2. Advertenties
    3. Domeinnamen
    4. Websites
    5. Design & lay-outs
    6. Scripts
    7. Overige
  6. Design
  7. Leden
    1. Actieve bezoekers
    2. Team
    3. Leden zoeken
  8. Downloads
  9. Goedkope domeinnamen
  1. Dé plek voor IT - ICTscripters
  2. Forum
  3. Scripting & programmeren
  4. PHP + SQL

Forum

  • Beta-testers gezocht voor Crypto-oefenplatform

    Syntax 29 januari 2026 om 16:11
  • Na 15 jaar terug van weggeweest: iCriminals.nl is terug (BETA)!

    Syntax 19 januari 2026 om 09:34
  • Developer Gezocht

    Mikevdk 10 januari 2026 om 18:57
  • Op zoek naar de legends

    Syntax 5 januari 2026 om 13:50
  • [FREE] WeFact Hosting module

    Jeroen.G 13 oktober 2025 om 14:09
  • Help testers nodig voor android app Urgent

    urgentotservices 26 september 2025 om 10:21
  • Versio vervanger

    Jeroen.G 25 augustus 2025 om 15:56
  • Afspraken systeem met planbeperking

    Lijno 1 augustus 2025 om 23:04

Marktplaats

  • 350 Nieuwe Domeinnamen Januari 2026

    shiga 1 februari 2026 om 14:21
  • 321 Nieuwe Domeinnamen December 2025

    shiga 1 januari 2026 om 10:26
  • Meerdere mafia game template te koop

    Syntax 26 december 2025 om 00:07

Closure probleem

  • M.Beers
  • 15 juni 2015 om 00:32
  • M.Beers
    Elite members
    Ontvangen Reacties
    31
    Berichten
    460
    • 15 juni 2015 om 00:32
    • #1

    Hallo allen,

    Ik wil d.m.v. de __call() methode een shortcut schrijven voor mijn HTML helper alleen krijg ik de volgende fout:

    Object of class Closure could not be converted to string in ...

    Alleen ik kan niet plaatsen wat ik nu fout doe gezien ik te weinig met Closures werk en ik er na 2,5 uur nog steeds niet uitkom. Zover mijn kennis loopt zou hij dit moeten doen maar schijn bedriegt... p.s. ik weet dat ik in de __call() methoden nog argumenten moet verwerken etc.

    Alvast bedankt,

    Michael

    Code
    <?php  
       /**
         * Magic shortcut to the custom HTML macros.
         *
         * @param string $name
         * @param array   $arguments
         * @return string
         */
        public function __call($name, array $arguments) {
            if (!isset(static::$tags[$name])) {
                throw new BadMethodCallException(vsprintf("Call to undefined method %s::%s().", [__CLASS__, $name]));
            }
            
            // Returnt een closure
            return static::$tags[$name];
        }
    ?>
    Toon Meer
    PHP: Testbestand
    <?php
    $html = new HTML();
    
    
    // Registreer een nieuwe html tag
    HTML::registerTag('foo', function($html, $content = null, $attributes = []) {
        return $html->tag('foo', $attributes, $content);
    });
    
    
    var_dump(HTML::$tags, $html->foo());
    ?>
    Toon Meer
    PHP: Hele HTML Helper
    <?php
    
    
    namespace Sparky\Util;
    
    
    use Closure;
    use BadMethodCallException;
    
    
    /**
     * HTML Helper.
     *
     * @package Sparky
     * @author Sparky Dev Team
     * @copyright Copyright (c) 2015 - 2015, Michael Beers | Online Media en Design (http://michaelbeers.nl)
     * @license http://opensource.org/licenses/Apache-2.0 Apache 2.0
     * @link http://michaelbeers.nl
     * @sice Version 1.0.0
     * 
     * @TODO Add a shortcut to call tags?
     */
    class HTML {
    
    
        /**
         * Should we return XHTML?
         *
         * @var boolean
         */
        protected $xhtml;
    
    
        /**
         * Custom tags.
         *
         * @var array
         */
        public static $tags = [];
    
    
        /**
         * Constructor.
         *
         * @param boolean $xhtml
         */
        public function __construct($xhtml = false) {
            $this->xhtml = $xhtml;
        }
    
    
        /**
         * Magic shortcut to the custom HTML macros.
         *
         * @param string $name
         * @param array   $arguments
         * @return string
         */
        public function __call($name, array $arguments) {
            if (!isset(static::$tags[$name])) {
                throw new BadMethodCallException(vsprintf("Call to undefined method %s::%s().", [__CLASS__, $name]));
            }
            
            return static::$tags[$name];
        }
    
    
        /**
         * Registers a new HTML tag.
         *
         * @param string $name
         * @param \Closure $tag
         */
        public static function registerTag($name, Closure $tag) {
            static::$tags[$name] = $tag;
        }
    
    
        /**
         * Takes an array of attributes and turns it into a string.
         *
         * @param array $attributes
         * @return string
         */
        protected function attributes($attributes) {
            $attr = '';
    
    
            foreach ($attributes as $attribute => $value) {
                if (is_int($attribute)) {
                    $attribute = $value;
                }
    
    
                $attr .= ' ' . $attribute . '="' . $value . '"';
            }
    
    
            return $attr;
        }
    
    
        /**
         * Creates a HTML5 tag.
         *
         * @param string $name
         * @param array $attributes
         * @param string $content
         * @returnstring
         */
        public function tag($name, array $attributes = [], $content = null) {
            return '<' . $name . $this->attributes($attributes) . (($content === null) ? ($this->xhtml ? ' />' : '>') : '>' . $content . '</' . $name . '>');
        }
    
    
        /**
         * Helper method for building media tags.
         *
         * @param string $type
         * @param mixed $files
         * @param array $attributes
         */
        protected function buildMedia($type, $files, $attributes) {
            $sources = '';
    
    
            foreach ((array) $files as $file) {
                $sources .= $this->tag('source', ['src' => $file]);
            }
    
    
            return $this->tag($type, $attributes, $sources);
        }
    
    
        /**
         * Creates audio tag with support for multiple sources.
         *
         * @param mixed $files
         * @param array $attributes
         */
        public function audio($files, array $attributes = []) {
            return $this->buildMedia('audio', $files, $attributes);
        }
    
    
        /**
         * Creates video tag with support for multiple sources.
         *
         * @param mixed $files
         * @param array $attributes
         */
        public function video($files, array $attributes = []) {
            return $this->buildMedia('video', $files, $attributes);
        }
    
    
        /**
         * Helper method for building list tags.
         *
         * @param string $type
         * @param mixed $items
         * @param array $attributes
         */
        protected function buildList($type, $items, $attributes) {
            $list = '';
    
    
            foreach ($items as $item) {
                if (is_array($item)) {
                    $list .= $this->tag('li', [], $this->buildList($type, $item, []));
                } else {
                    $list .= $this->tag('li', [], $item);
                }
            }
    
    
            return $this->tag($type, $attributes, $list);
        }
    
    
        /**
         * Builds an un-ordered list.
         *
         * @param array $items
         * @param array $attributes
         * @return string
         */
        public function ul(array $items, array $attributes = []) {
            return $this->buildList('ul', $items, $attributes);
        }
    
    
        /**
         * Builds am ordered list.
         *
         * @param array $items
         * @param array $attributes
         * @return string
         */
        public function ol(array $items, array $attributes = []) {
            return $this->buildList('ol', $items, $attributes);
        }
    
    
    }
    Toon Meer

    Dit was mijn spreekbeurt, zijn er nog vragen?

  • Guest, wil je besparen op je domeinnamen? (ad)
  • Thisguyisgone
    Elite members
    Ontvangen Reacties
    197
    Berichten
    7.880
    • 15 juni 2015 om 10:22
    • #2

    Bekijk deze reactie is http://php.net/manual/en/language.oop5.magic.php#63863 ;)

    Bewerkt één keer, laatst door Thisguyisgone (15 juni 2015 om 11:49).

  • M.Beers
    Elite members
    Ontvangen Reacties
    31
    Berichten
    460
    • 15 juni 2015 om 13:13
    • #3

    Daar gaat het niet fout ... wanneer ik het volgende uitvoer:

    PHP
    <?php
    $html = new HTML();
    $html->tag('foo', [] 'hello');
    ?>

    Krijg ik terug: <foo>Hello</foo>
    Dat werkt dus... (is ook een string)

    Maar wanneer ik dit vervolgens uitvoer in een anonymous function werkt het niet meer en krijg ik die error hierboven...

    http://php.net/manual/en/functions.anonymous.php

    Daarom vind ik het vreemd en is het ook niet te herleiden

    Opgelost call_user_func_array() ertussen en het werkt :P

    Zo simpel en niet aan gedacht evengoed bedankt Ferhat

    Edit:

    PHP
    <?php 
       /**
         * Magic shortcut to the custom HTML macros.
         *
         * @param string $name
         * @param array   $arguments
         * @return string
         */
        public function __call($name, array $arguments) {
            if (!isset(self::$tags[$name])) {
                throw new BadMethodCallException(vsprintf("Call to undefined method %s::%s().", [__CLASS__, $name]));
            }
            
            return  call_user_func_array(self::$tags[$name], $arguments);
        }
    Toon Meer

    Dit was mijn spreekbeurt, zijn er nog vragen?

  • FangorN
    Professional
    Ontvangen Reacties
    196
    Articles
    2
    Berichten
    737
    • 15 juni 2015 om 22:58
    • #4

    Bij dit soort vraagstukken kan het handig zijn om (in het vervolg) de PHP-versie die je gebruikt te vermelden omdat bepaalde constructies simpelweg niet werken in/voor bepaalde versies.

    Zo had ik een tijd geleden het probleem dat ik geen statische methoden kon aanroepen waarbij de klassenaam variabel was. Dit bleek niet ondersteund te worden voor PHP 5.3.0. Ook daar was call_user_func() de oplossing.

    Of een PHP upgrade lol.

  • M.Beers
    Elite members
    Ontvangen Reacties
    31
    Berichten
    460
    • 16 juni 2015 om 14:27
    • #5
    Citaat van FangorN

    Bij dit soort vraagstukken kan het handig zijn om (in het vervolg) de PHP-versie die je gebruikt te vermelden omdat bepaalde constructies simpelweg niet werken in/voor bepaalde versies.

    Zo had ik een tijd geleden het probleem dat ik geen statische methoden kon aanroepen waarbij de klassenaam variabel was. Dit bleek niet ondersteund te worden voor PHP 5.3.0. Ook daar was call_user_func() de oplossing.

    Of een PHP upgrade lol.

    Ik test mijn code in Travis CI dus in dit geval was dit niet van toepassing want ik test met PHP 5.4 tm 7 en HHVM

    Dit was mijn spreekbeurt, zijn er nog vragen?

  • FangorN
    Professional
    Ontvangen Reacties
    196
    Articles
    2
    Berichten
    737
    • 21 juni 2015 om 18:25
    • #6

    Het zou niet van toepassing zijn geweest als je dit had vermeld.

Participate now!

Heb je nog geen account? Registreer je nu en word deel van onze community!

Maak een account aan Login

ICT Nieuws

  • Fijne feestdagen

    tcbhome 28 december 2025 om 13:55
  • Kritieke update voor Really Simple Security-plug-in

    K.Rens 16 november 2024 om 16:12
  • ING Nederland streeft naar ondersteuning van Google Pay tegen eind februari

    K.Rens 2 november 2024 om 16:09

Blogs

  • Functioneel ontwerp

    Dees 28 december 2014 om 12:38
  • Access Control List implementatie in PHP/MySQL - deel 1/2

    FangorN 28 december 2018 om 12:35
  • Access Control List implementatie in PHP/MySQL - deel 2/2

    FangorN 29 december 2018 om 12:37

Gebruikers die dit topic bekijken

  • 2 Gasten
  1. Marktplaats
  2. Design
  3. Voorwaarden
  4. Ons team
  5. Leden
  6. Geschiedenis
  7. Regels
  8. Links
  9. Privacy Policy
ICTscripters ©2005 - 2026 , goedkope hosting door DiMoWeb.com, BE0558.915.582
Sponsors: Beste kattenhotel provincie Antwerpen | Beste Zetes eid kaartlezer webshop
Style: Nexus by cls-design
Stylename
Nexus
Manufacturer
cls-design
Licence
Commercial styles
Help
Supportforum
Visit cls-design