PHP awkward function declaration [duplicate]

Asked
Active3 hr before
Viewed126 times

8 Answers

function
90%

Stack Overflow Public questions & answers ,What does this kind of function declaration &get_instance() mean? Can the function name be a variable?,It means that the result of get_instance() is being returned by reference. Since objects are always by reference since PHP 5, it doesn't make sense to write code like that anymore. Incidentally, the public is also particularly curious, since that means this is PHP 5 code., 1 @gopi1410: A Singleton, putting it short, is forcing an object to have a single instance. And that instance will be available globally across the application. This has multiple disadvantages, and little advantages. It is considered harmful. – Madara's Ghost Jun 8 '12 at 20:04

It means that the result of get_instance() is being returned by reference. Since objects are always by reference since PHP 5, it doesn't make sense to write code like that anymore. Incidentally, the public is also particularly curious, since that means this is PHP 5 code.

get_instance()
load more v
88%

An object copy is created by using the clone keyword (which calls the object's __clone() method if possible). , Once the cloning is complete, if a __clone() method is defined, then the newly created object's __clone() method will be called, to allow any necessary properties that need to be changed. , When an object is cloned, PHP will perform a shallow copy of all of the object's properties. Any properties that are references to other variables will remain references. , It is possible to access a member of a freshly cloned object in a single expression:

$copy_of_object = clone $object;
load more v
72%

A user-defined function declaration starts with the word function:,In PHP, arguments are usually passed by value, which means that a copy of the value is used in the function and the variable that was passed into the function cannot be changed.,Note: A function name must start with a letter or an underscore. Function names are NOT case-sensitive.,Tip: Give the function a name that reflects what the function does!

 {
    echo "Hello World!";
 }
65%

CoffeeScript Logo ,CoffeeScript on GitHub,CoffeeScript is a little language that compiles into JavaScript. Underneath that awkward Java-esque patina, JavaScript has always had a gorgeous heart. CoffeeScript is an attempt to expose the good parts of JavaScript in a simple way.,Node 7+ required for CoffeeScript 2.

# Install locally
for a project:
   npm install--save - dev coffeescript

# Install globally to execute.coffee files anywhere:
   npm install--global coffeescript
load more v
75%

Functions Function Name Function Prefix Function Call Function Arguments Function Declaration Function Return ,This section describes the format for function names, calls, arguments and declarations.,Function call MUST NOT have a space between function name and open parenthesis e.g. func(); ,Function call MUST NOT have a space between function name and open parenthesis.

< ? php

// your code

// EOF
load more v
40%

Constants are defined by a function call taking a string; before that, they don’t exist. (This may actually be a copy of Perl’s use constant behavior.),Classes are not objects. Any metaprogramming has to refer to them by string name, just like functions.,PHP outright encourages “sanitizing”: there’s an entire data filtering extension for doing it.,echo is a statement-y kind of thing, not a function.

1
load more v
22%

When you try to use a global you must specify the global keyword first. You have specified it here when defining its value, but outside of that scope it needs to be redeclared as a global scope variable.,Similarly, you need to make sure those variables that you want to be globalized will be declared and assigned with corresponding values every page loaded.,The benefits are that now there is a documentation to what the former global does, and you have an obvious point for debugging when the value being returned is not the one you expect.,This gives you the same behavior of a global but with the advantage of having an assured initialization every time you access it.

e.g. in functions.php :

    function test() {
       global $hello;
       $hello = 'hello world';
    }
    add_action('after_setup_theme', 'test');

In single.php, this will not work:

    echo $hello;

Because $hello is undefined. This however will work:

    global $hello;
    echo $hello;

For example, here is a means of doing something similar via static variables e.g.

    function awful_function($new_hello = '') {
       static $hello;
       if (!empty($new_hello)) {
          $hello = $new_hello;
       }
       return $hello;
    }

    awful_function('telephone');
    echo awful_function(); // prints telephone
    awful_function('banana');
    echo awful_function(); // prints banana

If you really want to save time by storing data somewhere to re-use, consider using the WP_Cache system with wp_cache_get etc e.g.

$value = wp_cache_get('hello');
if (false === $value) {
   // not found, set the default value
   wp_cache_set('hello', 'world');
}
load more v
60%

The most important difference between class- and prototype-based inheritance is that a class defines a type which can be instantiated at runtime, whereas a prototype is itself an object instance.,To put a really fine point on that, a child of a prototype isn’t a copy of its prototype, nor is it an object with the same shape as its prototype. The child has a living reference to the prototype, and any prototype property that doesn’t exist on the child is a one-way reference to a property of the same name on the prototype.,This isn’t just about avoiding the inherent ugliness of inheritance, or enforcing encapsulation. Think about what else you might do with secretFactory and leaker that you couldn’t easily do with a prototype or a class.,A child of an ES6 class is another type definition which extends the parent with new properties and methods, which in turn can be instantiated at runtime. A child of a prototype is another object instance which delegates to the parent any properties that aren’t implemented on the child.

JavaScript Pop Quiz #1: What’s the Essential Difference Between These Code Blocks?

function PrototypicalGreeting(greeting = "Hello", name = "World") {
   this.greeting = greeting
   this.name = name
}

PrototypicalGreeting.prototype.greet = function() {
   return `${this.greeting}, ${this.name}!`
}

const greetProto = new PrototypicalGreeting("Hey", "folks")
console.log(greetProto.greet())
load more v

Other "function-undefined" queries related to "PHP awkward function declaration [duplicate]"