Asked  2 Years ago    Answers:  5   Viewed   54 times

What I would like to do is something like this:

$method_result = new Obj()->method();

Instead of having to do:

$obj = new Obj();
$method_result = $obj->method();

The result doesn't actually matter to me in my specific case. But, is there a way to do this?



The feature you have asked for is available from PHP 5.4. Here is the list of new features in PHP 5.4:

And the relevant part from the new features list:

Class member access on instantiation has been added, e.g. (new Foo)->bar().

Monday, October 17, 2022

First of all, __get, __set, etc. are defined public and you cannot have them otherwise. Magic methods should be used wisely as it takes about three times as long to make a call to a magic method than simply calling a class method.

class A {
   public function __get($name) { ... }

   public function __getValue() { ... }     // <== is faster


Usually (normally, preferably), you will have your class members private or protected (never public) and have accessors and mutators to encapsulate them. Those accessors and mutator may be of any visibility, depending on what the user can do with the members. You may also have immutable classes by declaring only accessors for your members, which are initialized in the constructor only.

So, your sample class should read

class classWithReadOnlyVar {
   private $readOnlyVar;

   public function getReadonlyVar() {
     return $this->readOnlyVar;


and should not use the magic methods.

There may be many reasons to avoid using magic methods at all :

  1. they break code completion
  2. they are slower at run-time
  3. they make refactoring and maintenance a bit (lot) harder/complicated
  4. you can't have a protected magic method
  5. etc.

Class members

Their visibility should be private or protected, it all depends if you want them accessible by inheritence. They should never be public as this breaks the OO paradigm.

Example of a protected member:

class A {
    protected $_name = 'A';

    public function getName() { return $this->_name; }

class B {
    protected $_name = 'B';   // getName() will not return 'B'

(without $_name being protected, this would not be possible, and no need to redefine the accessor)


They should be protected or public. Having a private accessor makes no sense; a class should access it's member directly. If the member needs processing, the class will know regardless when to call the accessor or not.


They should be protected or public. As for the accessors, it makes no sense to have a private mutator... unless a very rare case when processing needs to be done internally. If a class member has no accessor, it should not have a mutator. It also makes no sense to have a mean to set a value without being able to get the value back somehow.

Sunday, October 2, 2022

I think the correct syntax for referencing ranges of cells in another sheet is:


So you should try:

$objValidation->setFormula1('List!$A$1:$A$10'); // tested it, worked for me

Got the idea from

->setFormula1("Worksheet!A1:{$endCell}1");// work....

Although this guy had another problem with using named ranges.

Background: I think with:


you're explicity using the given string between the quotation marks as the list value as explained here: here (where you probably got this snippet in the first place) or here. But since you don't want to use fixed list items but dynamically referred ones, you should omit the double quotation marks.

Friday, October 14, 2022

Take a look at this link this covers the answer in detail.

You will need to ensure all machines and separate applications on the site share a common (but unique to production) machine key to allow the authentication cookies to be trusted by all the machines/applications.

If you are simply using virtual directories under the same sub domain then simply harmonising the web.conig Forms Auth settings and machine keys should get you up and running very quickly.

If you want this to work between a second level domain then you need to change the "Domain" setting on the Form's Auth cookie. See the article for details.

Wednesday, August 3, 2022

If data is your dataframe, you can get the mean of all the columns as integers simply with:

data.mean().astype(int)  # Truncates mean to integer, e.g. 1.95 = 1

or, as of version 0.17.0:

data.mean().round(0)  # Rounds mean to nearest integer, e.g. 1.95 = 2 and 1.05 = 1
Wednesday, August 3, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :

Browse Other Code Languages