Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I Use Inner Classes in PHP?

I'm from a Java background, and I want to use an inner class in php. Every time I put the inner class though, I get a syntax error. Is this possible with PHP? Also, how do I reference the outer class? Do I get access to ALL its data members?

<?php  class OuterClass {     var $x = 15;     function __construct() {      }      class InnerClass {                  // error when InnerClass is static         function __construct() {        // error when InnerClass is static             echo $x;         }     } }  ?> 

This is used for a MoveClass (as in make a move) of a specific card game. I think it'd be good design to put these classes together because they don't make sense apart. Also, the MoveClass needs to know about some data members of the Game class. Why not make it a function? It's simply too big.

Edit:

What about nested classes? From what I understand, those have to be static? O_o

like image 554
Lindz Avatar asked Dec 04 '10 03:12

Lindz


People also ask

What is inner class in PHP?

In object-oriented programming (OOP), an inner class or nested class is a class declared entirely within the body of another class or interface. It is distinguished from a subclass. – Wikipedia. So basically inner class is a class that is declared inside the scope of another class.

Can we create class inside class in PHP?

PHP 7 has introduced a new class feature called the Anonymous Class which will allow us to create objects without the need to name them. Anonymous classes can be nested, i.e. defined inside other classes. Read this article to learn how to defined and use anonymous classes nested inside other classes.

How do you execute an inner class?

Inner classesTo instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax: OuterClass. InnerClass innerObject = outerObject.

Can inner classes be used?

We use inner classes to logically group classes and interfaces in one place to be more readable and maintainable. Additionally, it can access all the members of the outer class, including private data members and methods.


2 Answers

PHP does not allow for inner classes. Should you wish to access all of the data members from the parent class, I would suggest you employ Inheritance.

A possible alternative:

class OuterClass {     var $x = 15;     function __construct() {      } } class ChildClass extends OuterClass {     function __construct() {         parent::__construct();     } } 

You can envoke a method form the parent class by referring to the class itself; In PHP you can do this with the parent keyword. So, to refer to a method in the context of a class rather than an object we use :: as opposed to ->.

like image 199
Russell Dias Avatar answered Sep 19 '22 21:09

Russell Dias


in PHP 5.4 or later, you can use PHP Traits which are designed more for multiple inheritance, but may suit your needs. From the PHP Documentation:

A Trait is similar to a class, but only intended to group functionality in a fine-grained and consistent way. It is not possible to instantiate a Trait on its own. It is an addition to traditional inheritance and enables horizontal composition of behavior; that is, the application of class members without requiring inheritance.

http://php.net/manual/en/language.oop5.traits.php

like image 37
Jason Avatar answered Sep 19 '22 21:09

Jason