0

I have problem, can i call constructor without create 'new class()' ? Or you maybe have another way for this :

<?php

class a
{
    public static $hello;

    public function say()
    {
        return self::$hello;
    }

}

class b extends a
{
    public function __construct()
    {
        self::$hello = 'hello world';
    }
}


echo b::say();

?>

I have try with :

$b = new b();
echo $b->say();

And it's work. But i want to use b::say();

Can help me?

Thank you!!

4
  • 2
    Why do you want to call a constructor without instantiating the class? Given that this is the purpose of a constructor in the first place... if you want to work purely with static methods, then don't use the constructor to set static properties Commented May 4, 2016 at 12:05
  • @MarkBaker because i will make a many class in different file with different value of $hello variable. so i just call like b::say(); c::say(); etc. it's not real code in my project, but the logic is like that for solve my problem. sorry, my english not good. thank you! Commented May 4, 2016 at 12:15
  • If you're making many identical classes, just with different values of $hello, then I think you've misunderstood what OO is all about Commented May 4, 2016 at 12:26
  • Answered the question and worked :) Hope I could help you @kresek Commented May 4, 2016 at 12:30

4 Answers 4

1

Check out this. Is this good for you?

<?php

class a {
    public static $hello;

    public static function say() {
        return self::$hello;
    }
}

class b extends a {
    public function __construct() {
        self::$hello = 'hello world';
    }

    public static function factory() {
        return new b();
    }
}

echo b::factory()->say();

?>

Actually I couldn't find a way to do this without calling constructor. This is how the workaround looks like. factory is just a name. you can rename it.

calling class method (with constructors) without object instantiation in php

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for answer. I'll try it. Maybe i can change to 'b::say()->factory();'.
0

You have asked: "can i call constructor without create 'new class()' ?"
The answer: No.

... Classes which have a constructor method call this method on each newly-created object

You have requested "But i want to use b::say();"
b::say(); - is call of static method.
You can't override non-static parent method to static. But you can restructure your base class class a to make say() method static.

1 Comment

Hey, Thanks for answer. Yes, my question is like that. But not must call a constructor. I search for another way. :)
0
<?php

class a
{
  public static $hello;

public static function say()
{
  return self::$hello;
}

}

class b extends a
{
public function __construct()
  {
      self::$hello = 'hello world';
  }
}

The thing that you were missing was you needed to add your content to your new class method. - Just call it like so:

$b = new b('Some words');
echo $b->say();

When calling a new class and using a constructor - You will want to add the content in the paramaters for the new class you are making.

It acts as if you are calling the __construct function. - Calling new class($a) will call the __construct($a) function once making the object.

Hope that this helped a bit :)

4 Comments

Haven't tested the code yet, the self::hello might not work.
Nevermind removing it now. - I tested with the $ and worked for me.
Thank you for answer, jek! But in my question, i no want to use $b = new b(). i want just call b::say(); :D i'll update why i want using like that.
Oh sure thing! Sorry about that :') It's not possible to do that with OOP using just static methods - Static methods are supposed to be called using parameter's in the proper way of using it. I haven't dwelled with this stuff before but I hope someone can help you out! :)
0

Yes it is possible just make the say() function static like this :

public static function say()
{
    return self::$hello;
}

Declaring class methods as static makes them accessible without needing an instantiation of the class.

This example looks to me like late static binding. So try changing that return self::$hello; into return static::$hello;

4 Comments

I tried this as I was trying to answer and for me it displays nothing?
because $hello does not hold anything.
Hey thanks for answer. i've try before, but not work.
Read more about static. To explain in simple word, calling a static function it's like calling a new instantiation without a constructor so that $hello variable does not have any value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.