• Inloggen
  • Registreer
  • Zoeken
Dit topic
  • Overal
  • Dit topic
  • Dit forum
  • Artikelen
  • Pagina's
  • Forum
  • Downloads
  • Meer opties

ICTscripters

Dé plek voor IT

Dé plek voor IT

Aanmelden

Geavanceerde opties
  1. Home
  2. Forum
    1. Alle berichten
    2. Recente activiteiten
    3. Onopgeloste topics
  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
  1. Home
  2. Forum
    1. Alle berichten
    2. Recente activiteiten
    3. Onopgeloste topics
  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
  1. Home
  2. Forum
    1. Alle berichten
    2. Recente activiteiten
    3. Onopgeloste topics
  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
  1. Dé plek voor IT - ICTscripters
  2. Forum
  3. Scripting & programmeren
  4. PHP + SQL

Forum

  • nieuwe game

    tigermaffia 31 oktober 2026 om 23:00
  • Criminolz

    qevanto 31 oktober 2026 om 20:52
  • [Beta] Ottomaan: gratis browserspel met een echte, levende stad. Feedback gezocht!

    frio 27 oktober 2026 om 12:22
  • Port Father: beta van een browsergame in Antwerpen 1931, feedback welkom

    Jeroen.G 26 oktober 2026 om 10:33
  • Maffia Hotel: een browsergame waarin je van lobbygast naar spil van het hotel groeit

    eeyk 22 oktober 2026 om 18:43
  • Crime Club volledig herschreven vanaf nul

    frio 19 oktober 2026 om 23:57
  • Malavita (nieuwe maffia game, van 0)

    jos 19 oktober 2026 om 23:18
  • Herkent deze Notion-structuurscan echte problemen?

    Jeroen.G 19 oktober 2026 om 15:09

Marktplaats

  • [Aanbod] Stadskaart voor je browsergame: van menu naar klikbare stad, vanaf €200

    frio 29 oktober 2026 om 18:17
  • [Verkoop] G4G (Gangsters 4 Gangsters): volledige broncode, nieuw gebouwd in PHP 8.3 | € 30 per domein

    frio 27 oktober 2026 om 18:54
  • [TE KOOP] Malavita.be [Maffiaspel, gebaseerd op omicidio.nl, unieke code]

    Dpm_x 26 oktober 2026 om 00:22

Closure probleem

  • M.Beers
  • 15 juni 2015 om 00:32
  • M.Beers
    Elite members
    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];
        }
    ?>
    Meer tonen
    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());
    ?>
    Meer tonen
    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);
        }
    
    
    }
    Meer tonen

    Dit was mijn spreekbeurt, zijn er nog vragen?

  • Gast, wil je besparen op je domeinnamen? (ad)
  • Thisguyisgone
    Elite members
    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
    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);
        }
    Meer tonen

    Dit was mijn spreekbeurt, zijn er nog vragen?

  • FangorN
    Professional
    Artikelen
    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
    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
    Artikelen
    2
    Berichten
    737
    • 21 juni 2015 om 18:25
    • #6

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

Registratie

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

Nu registreren Aanmelden

ICT Nieuws

  • PHP waarschuwt voor twee ernstige bugs die makkelijk uit te buiten lijken

    Aaron 22 oktober 2026 om 22:19

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. Privacyverklaring
  10. Contact
ICTscripters ©2005 - 2026 , goedkope hosting door DiMoWeb.com, BE0558.915.582
Sponsors: Beste kattenhotel provincie Antwerpen
Style: Nexus by cls-design
Stylename
Nexus
Manufacturer
cls-design
Licence
Commercial styles
Help
Supportforum
Visit cls-design