I've read tutorial on tutorial and I try to work with Object Oriented PHP but the thing is that I find it rather complex. When I create a class I must include it in the current working document. It doesn't work as in Java when the compiler finds the script in the same folder anyway. And when I always must include that class in different directories it gets really inefficient. Is there a way to achieve what I'm looking for? Or have I completely misunderstood the concept in PHP?

Today I have a script which automatically fixes the correct directory relative from the current working directory and return to me an array with paths so that I can write...


To get the path to the db_class. How should I proceed to achieve effective programming? And how exactly do I use :: ?

Clotoss commented: Very good at explaning things. +0

Recommended Answers

All 7 Replies

What are you using as an editor? Notepad ++? And are you testing the code on a website, or some other type of software?

I believe what you are looking for is autoloading, which php does have. http://php.net/manual/en/language.oop5.autoload.php

As far as the :: are you referring to the context of self:: static:: etc or are you referencing namepspaces as the use of :: in namespaces was dropped in lieu of \

I'm using Netbeans IDE 6.9.1. The autoloading class is exactly what I'm looking for! But would it be better to include multiple classes into the same php file? What is the most effective way to handle classes and inclusion? How do you guys handle inclusions when working with classes dependent upon another in different directories?

And I don't really know what I mean with ::, all I know is that the source code to wordpress is filled with them x)

lol :P

PHP is an open-source server-side scripting language widely used by developers to develop projects. Most of the time, developers intentionally and unintentionally don't bother much and usually never follow conventional practices. It is important to follow the best practices for every piece of code you write for a project. Here is what you should start practicing in the next projects for effective PHP development.

1) Maintain Proper Documentation of the Code: Try to comment on the codes to maintain proper documentation. It will help you remember the code when you will review it in the future.

2) Create a Configuration File: Create a configuration file for managing database connections and declaring variables. It's helpful when you want to change the database details and use variables globally in the project. Just by including the files, you can use the same files anywhere in the project.

3) Don't Put phpinfo() in the Root Directory: Most of the time, developers use the phpinfo() function in the root directory. It is a bad practice as it opens various security loopholes for hackers. Instead, you should put a file with phpinfo() on the server to track information about the server environment.

4) Use Functions and Classes: Try to use functions and classes in the projects. It helps the developers to use the same functions anywhere in the project by calling the objects of the classes.

5) Turn Error Reporting On: The error-reporting() functions in the PHP ini file display the run time errors of the website. So turning the default settings ON helps the developers to fix the server's errors as soon as they appear.

6) Use Frameworks: The frameworks usually work on MVC( Model View Controller) architectures like CakePHP, Zend, Laravel, CodeIgnitor, and more. So use these standardized platforms to build the websites.

This is how you can practice effective PHP web development!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.