3464

is there anyway to define public or private or protected classes in PHP

Is there anyway to create public or private or protected classes in a namespace just like java?

like...

namespace foo; public class Account { ....... } private class PrivateAccount { ....... }

please let me know if there is any workaround to do this in PHP.

Answer1:

Typically what you do is declare the constructor of the class private. Then you can create a separate static function in the class that will do some checks (i.e. check the namespace) and conditionally return an instance of the class by calling the private constructor. This is also typically how you implement singletons.

Answer2:

No, classes in PHP may not be anything but public and that is declared implicitly. From the PHP docs on classes in PHP5:

Basic class definitions begin with the keyword class, followed by a class name, followed by a pair of curly braces which enclose the definitions of the properties and methods belonging to the class.

Putting a visibility keyword in front of the class will likely result in a fatal error.

Recommend

  • MVC3 - Conditionally add id element with Razor
  • Can I put condition in emacs lisp macro?
  • Application Express: Anonymous PL/SQL Block and Bind Variables
  • When querying against a view, a filtering clause in the view's definition is being ignored
  • iOS - Is this a task for enums?
  • mapping between two ontologies
  • DataGridView: Pass by Value or Reference?
  • Need code translation from VB to C#
  • Best practice to eliminate magic numbers within a member function
  • How can I replace the server in Web Component Tester
  • Can someone please explain to me in the most layman terms how to use EventArgs?
  • Spring boot 2.0.0.M4 required a bean named 'entityManagerFactory' that could not be found
  • Eliminate partial duplicate rows from result set
  • How can I set a binding to a Combox in a UserControl?
  • Create DicomImage from scratch using Dcmtk
  • SplFileObject error failed to open stream: No such file or directory
  • print() is showing quotation marks in results
  • Play WS (2.2.1): post/put large request
  • Refering to the class itself from within a class mehod in Objective C
  • Exception “firebase.functions() takes … no argument …” when specifying a region for a Cloud Function
  • Highlight one bar in a series in highcharts?
  • Control modification in presentation layer
  • Fetching methods from BroadcastReceiver to update UI
  • Does CUDA 5 support STL or THRUST inside the device code?
  • sending/ receiving email in Java
  • Upload files with Ajax and Jquery
  • Do I've to free mysql result after storing it?
  • Calling of Constructors in a Java
  • PHP: When would you need the self:: keyword?
  • A cron job substitute?
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Proper folder structure for lots of source files
  • Proper way to use connect-multiparty with express.js?
  • Angular 2 constructor injection vs direct access
  • Checking variable from a different class in C#
  • Django query for large number of relationships
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?
  • How to push additional view controllers onto NavigationController but keep the TabBar?