Giter VIP home page Giter VIP logo

Comments (9)

ThomasWeinert avatar ThomasWeinert commented on June 10, 2024

Why? If you need to remove the content you can send an empty string as value.

from fluentdom.

salarmehr avatar salarmehr commented on June 10, 2024

Why what? It's about making it compatible with jQuery API.
Consider the following example:

$dom->html($w)->attr('placeholser',$x)->$attr('maxlength',$y)->$att('title',$t); 

If any of $w,$t, .. variables is set to null the corresponding method will act as a getter, the chain will break and an exception will be thrown. This is not the case in jQuery api.

from fluentdom.

ThomasWeinert avatar ThomasWeinert commented on June 10, 2024

You can always cast the variables.
$dom->html((string)$w)->attr('placeholder',(string)$x)...

PHP functions usually make no difference between NULL and undefined. NULL is for optional parameters (type hints, but internal functions, too).

In your example you provide the arguments. See this example:

function dump() {
  var_dump(func_get_args());
}
dump($a, $b);
array(2) {
  [0]=>
  NULL
  [1]=>
  NULL
}

from fluentdom.

salarmehr avatar salarmehr commented on June 10, 2024

You can easily solve that fundamentally and there is no need to add a (string) everywhere.

function attr()
{
    if (count(func_get_args()) < 2) {
        return 'I am a getter!';
    };
    return 'I am a setter!!';
}

echo attr('type').PHP_EOL;
echo attr('type',null).PHP_EOL;
echo attr('type','foo');
I am a getter!
I am a setter!!
I am a setter!!

from fluentdom.

ThomasWeinert avatar ThomasWeinert commented on June 10, 2024

Yes, but even an undefined variable like in my example is treated as NULL. I will think about it, but I am a little afraid how much code (that uses FD) would break.

from fluentdom.

salarmehr avatar salarmehr commented on June 10, 2024

I don't know what you are trying to show by that example, how your example is related to this issue.. May you please provide an example of breaking code?

from fluentdom.

ThomasWeinert avatar ThomasWeinert commented on June 10, 2024

Some source that wraps or extends the current methods. It would expect the current behavior. If a method extends FluentDOM:attr() and calls the parent method expecting the current behavior it will break.

from fluentdom.

ThomasWeinert avatar ThomasWeinert commented on June 10, 2024

Variadics allow for a cleaner implementation of optional arguments (then func_get_args()) and even for generic wrapper functions:

function attr($name, ...$arguments) {
  var_dump($arguments);
}

function attributes($name, ...$arguments) {
  attr($name, ...$arguments);
}

attributes('one');
attributes('two', '21');

from fluentdom.

salarmehr avatar salarmehr commented on June 10, 2024

Well done. Thanks.

from fluentdom.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.