开发者

How do I make 15 static PHP classes work together well?

开发者 https://www.devze.com 2022-12-11 23:27 出处:网络
I have a personal PHP application that has about 15 classes. Each class is only initiated once as a page is executed. In other words, when the page loads:

I have a personal PHP application that has about 15 classes. Each class is only initiated once as a page is executed. In other words, when the page loads:

  • 15 classes are loaded, and as each class file gets loaded, I create one instance of the class.
  • The application (so far) is designed so every variable in the 开发者_StackOverflowsystem has one state during the generation of a page. I use global vars to access each of these
  • It's worked fine for 3 years, but I am the sole developer and a goo debugger of my own code.

I have heard all the issues with Singletons, and I hate doing, "global $var" all over the place. Please tell me how to pull this type of structure out and into something developers would love. I want to write software the right way, but I can't seem to find a very simple code framework for this type of execution.

Oh - and I'm not looking for a MVC framework solution. I would love your thoughts on how I take 15 classes and turn them into a proper framework for working together. I would also love an articulation on how "stupid" it is to develop this way.


If you have variables you want all classes to have access to, maybe you could try inheritance, for example something like

class Settings
{
    var $page_name = "My Page";
    var $database_name = "my_db";
}

and then let all classes inherit this class like

class Page extends Settings
{
    var $id = 0;
    var $template = "";
    function __construct() {}
}

$page = new Page();
echo $page->page_name;

or you could define the values as constants, if that is what they are

define("PAGE_NAME", "My Page");

you would have access to PAGE_NAME everywhere

I'm not in any way saying this is the right way, it's just a way. :)


I ran into the same issue when I was developing php apps. What I ended up doing was creating classes of static methods and static variables. In essence they work somewhat like namespaces. I am not sure how "standard" this is but it works very well in practice.

The only other option would be to create a singleton like you mentioned in your question, but imho that can be overkill unless you need something like constructors and deconstructors.

Example:

<?php
class MyClass{
    private static $my_static_variable;

    static public function myStaticMethod(){
        return self::$my_static_variable;
    }
}

You can then use this anywhere without the need to use global:

function foo(){
    MyClass::myStaticMethod();
}

class Bar{
    public function myBarMethod(){
        MyClass::myStaticMethod();
    }
}


I have been considering the same thing myself, and the only conclusion I could come to way to crack open an MVC framework and see how it's built.

The other thing is as mentioned, to start with a single super class and inherit down from there.

So have a class Object, then inherit that into something and so on and so on.

0

精彩评论

暂无评论...
验证码 换一张
取 消