Is it possible to create static classes in PHP like in C

0 votes

I'm trying to make a static class in PHP behave like a C# class, so,

  • On the initial call to the class, the function Object() { [native code] } is immediately called.
  • No instantiation is necessary.

Something like this:

static class Hello {
    private static $greeting = 'Hello';

    private __construct() {
        $greeting .= ' There!';
    }

    public static greet(){
        echo $greeting;
    }
}

Hello::greet(); // Hello There!

 Can someone please help me with this?

Aug 4 in PHP by Kithuzzz
• 4,080 points
7 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In PHP

0 votes
1 answer

What is a Cookie? How to create Cookies With PHP?

A cookie is often used to identify ...READ MORE

answered Feb 13, 2020 in PHP by Niroj
• 82,740 points
2,130 views
0 votes
1 answer

How to Debug Variables like in PHP var_dump()?

Hello @kartik, Try out with the Smarty Session: {$smarty.session|@debug_print_var} or {$smarty.session|@print_r} To ...READ MORE

answered Apr 20, 2020 in PHP by Niroj
• 82,740 points
629 views
0 votes
1 answer

How to import all classes from another namespace in PHP?

Hello @kartik, This is not possible in PHP. All ...READ MORE

answered Oct 20, 2020 in PHP by Niroj
• 82,740 points
2,594 views
0 votes
1 answer

In PHP, how to detect the execution is from CLI mode or through browser ?

Hello @kartik, Use the php_sapi_name() function. if (php_sapi_name() == "cli") { ...READ MORE

answered Oct 22, 2020 in PHP by Niroj
• 82,740 points
278 views
0 votes
1 answer

How to check if a string is an email address in PHP?

Hello, Try this without regular expressions: <?php ...READ MORE

answered Nov 3, 2020 in PHP by Niroj
• 82,740 points
467 views
0 votes
1 answer

How to check an IP address is within a range of two IPs in PHP?

Hello @kartik, With ip2long() it's easy to convert your addresses ...READ MORE

answered Nov 3, 2020 in PHP by Niroj
• 82,740 points
1,913 views
0 votes
0 answers

Is it possible to write strictly typed PHP code?

Is it possible to write code like ...READ MORE

Jun 6 in PHP by Kichu
• 19,040 points
12 views
0 votes
0 answers

unset variable in php

I just read a PHP manual. that ...READ MORE

Jun 10 in JQuery by narikkadan
• 11,280 points
23 views
0 votes
0 answers

What does the variable $this mean in PHP?

Can someone please explain how the variable ...READ MORE

Jun 11 in PHP by narikkadan
• 11,280 points
14 views
0 votes
0 answers

Static variables in PHP

Can someone please explain to me what ...READ MORE

Jun 12 in PHP by narikkadan
• 11,280 points
21 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP