PHP abstract properties

Asked
Active3 hr before
Viewed126 times

10 Answers

abstractproperties
90%

However you can implement a getter/setter function abstract, this might be what you're looking for.,No, there is not. Properties cannot be declared abstract in PHP.,Fatal error: Properties cannot be declared abstract in ... on line 3,Is there any way to define abstract class properties in PHP?

No, there is no way to enforce that with the compiler, you'd have to use run-time checks (say, in the constructor) for the $tablename variable, e.g.:

class Foo_Abstract {
   public final
   function __construct( /*whatever*/ ) {
      if (!isset($this - > tablename))
         throw new LogicException(get_class($this).
            ' must have a $tablename');
   }
}

You could declare an abstract getter instead:

abstract class Foo_Abstract {
   abstract public
   function get_tablename();
}

class Foo extends Foo_Abstract {
   protected $tablename = 'tablename';
   public
   function get_tablename() {
      return $this - > tablename;
   }
}
load more v
88%

PHP has abstract classes and methods. Classes defined as abstract cannot be instantiated, and any class that contains at least one abstract method must also be abstract. Methods defined as abstract simply declare the method's signature; they cannot define the implementation. , When inheriting from an abstract class, all methods marked abstract in the parent's class declaration must be defined by the child class, and follow the usual inheritance and signature compatibility rules. ,Example #2 Abstract class example,Example #1 Abstract class example

ConcreteClass1
FOO_ConcreteClass1
ConcreteClass2
FOO_ConcreteClass2
load more v
72%

There is no such thing as defining a property.,You can only declare properties because they are containers of data reserved in memory on initialization. ,"Abstract" only indicates that something was declared but not defined and therefore before using it, you need to define it or it becomes useless.,A function on the other hand can be declared (types, name, parameters) without being defined (function body missing) and thus, can be made abstract.

No, there is no way to enforce that with the compiler, you'd have to use run-time checks (say, in the constructor) for the $tablename variable, e.g.:

class Foo_Abstract {
   public final
   function __construct( /*whatever*/ ) {
      if (!isset($this - > tablename))
         throw new LogicException(get_class($this).
            ' must have a $tablename');
   }
}
load more v
65%

I was just wondering if it was possible to create/emulate abstract properties in PHP.,This is for a Page class, which is extended for each page in the site. A skeleton in shown below:,I was mainly just interested in whether abstract properties are possible at all.,As far as I know, there would be no way to force a sub-class to implement the abstract property.

This is for a Page class, which is extended for each page in the site. A skeleton in shown below:

abstract class Page {
   protected $title;

   abstract
   function displayContent();

   protected
   function display() {
      // display page, calling displayContent somewhere, and using the title from above
      // loads a page template
   }

   private
   function __destruct() {
      $this - > display();
   }
}
load more v
75%

An abstract class cannot be instantiated. It provides an interface for other classes to extend.,An abstract class is a class that cannot be instantiated. Typically, an abstract defines an interface for other classes to extend.,An abstract class can have properties and methods as a regular class. But it cannot be instantiated.,Summary: in this tutorial, you will learn how to use the PHP abstract class to define an interface for other classes to extend.

To define an abstract class, you add the abstract keyword as follows:

.wp - block - code {
      border: 0;
      padding: 0;
   }

   .wp - block - code > div {
      overflow: auto;
   }

   .shcb - language {
      border: 0;
      clip: rect(1 px, 1 px, 1 px, 1 px); -
      webkit - clip - path: inset(50 % );
      clip - path: inset(50 % );
      height: 1 px;
      margin: -1 px;
      overflow: hidden;
      padding: 0;
      position: absolute;
      width: 1 px;
      word - wrap: normal;
      word - break: normal;
   }

   .hljs {
      box - sizing: border - box;
   }

   .hljs.shcb - code - table {
      display: table;
      width: 100 % ;
   }

   .hljs.shcb - code - table > .shcb - loc {
      color: inherit;
      display: table - row;
      width: 100 % ;
   }

   .hljs.shcb - code - table.shcb - loc > span {
      display: table - cell;
   }

   .wp - block - code code.hljs: not(.shcb - wrap - lines) {
      white - space: pre;
   }

   .wp - block - code code.hljs.shcb - wrap - lines {
      white - space: pre - wrap;
   }

   .hljs.shcb - line - numbers {
      border - spacing: 0;
      counter - reset: line;
   }

   .hljs.shcb - line - numbers > .shcb - loc {
      counter - increment: line;
   }

   .hljs.shcb - line - numbers.shcb - loc > span {
      padding - left: 0.75 em;
   }

   .hljs.shcb - line - numbers.shcb - loc::before {
      border - right: 1 px solid #ddd;
      content: counter(line);
      display: table - cell;
      padding: 0 0.75 em;
      text - align: right; -
      webkit - user - select: none; -
      moz - user - select: none; -
      ms - user - select: none;
      user - select: none;
      white - space: nowrap;
      width: 1 % ;
   } < ? php

abstract class className {
   // ...
}
Code language: HTML, XML(xml)
load more v
40%

Abstract classes and methods are when the parent class has a named method, but need its child class(es) to fill out the tasks.,The child class method must be defined with the same name and it redeclares the parent abstract method,Let's look at another example where the abstract method has an argument, and the child class has two optional arguments that are not defined in the parent's abstract method:,But, intro() is an abstract method that should be defined in all the child classes and they should return a string.

An abstract class is a class that contains at least one abstract method. An abstract method is a method that is declared, but not implemented in the code.

An abstract class or method is defined with the abstract keyword:

abstract
22%

You can only declare properties because they are containers of data reserved in memory on initialization. ,There is no such thing as defining a property.,Fatal error: Properties cannot be declared abstract in ... on line 3,The reason we would like abstract properties is to make sure that subclasses define them and throw exceptions when they don't. In my specific case, I needed something that could work with statically.

Is there any way to define abstract class properties in PHP?

abstract class Foo_Abstract {
   abstract public $tablename;
}

class Foo extends Foo_Abstract {
   //Foo must 'implement' $property
   public $tablename = 'users';
}
load more v
60%

abstract class Validator {,     * @var array An array containing names and types of abstract properties that must be implemented in child classes,public abstract class Foo {,    private $_abstract_properties = array(

What are abstract properties? There are abstract methods but the notion of an abstract property seems a bit ridiculous. Why so, you might ask! Well, the thing is, methods are declared & defined (not necessarily at same time). So you can declare a method without actually defining it (ie., no body, no set of commands which it will execute) and the same can be defined at a later stage. But there is no such thing as defining a property of a class; properties are always declared as containers of data which reserve a space in memory on class initialization.

As I was re-factoring data validation service in a project I’m working on (will open source it once first working draft is ready), I thought about making the $rules property abstract. But then I realized the absurdity of my thought and the fact that PHP does not have such a concept, not even like what C# has. From what little I know of C#, it has a concept of abstract properties which are compiled into functions by the compiler. So if I were to do this:

$rules
load more v
48%

Is there any way to define abstract class properties in PHP?,You can only declare properties because they are containers of data reserved in memory on initialization. ,There is no such thing as defining a property.,"Abstract" only indicates that something was declared but not defined and therefore before using it, you need to define it or it becomes useless.

Is there any way to define abstract class properties in PHP?

abstract class Foo_Abstract {
   abstract public $tablename;
}

class Foo extends Foo_Abstract {
   //Foo must 'implement' $property
   public $tablename = 'users';
}
23%

I am using PHP and designing some abstract classes.,I can declare properties with constant values within my abstract class and access/overwrite them from any class which extends this without re declaring them within any of the extending classes.,When designing an abstract class, you may want to ask yourself:,To answer your question, yes. If the abstract class is the logical place for the property then put it there. Use the tools that classes and OOP gives you.

If not many classes will inherit from it or it creates a relationship where there would otherwise be none then chances are you can do what you're trying to do with traits.

An example would be Model vs ActiveRecord; "form vs feature".

Model

If not many classes will inherit from it or it creates a relationship where there would otherwise be none then chances are you can do what you're trying to do with traits.

An example would be Model vs ActiveRecord; "form vs feature".

ActiveRecord
load more v

Other "abstract-properties" queries related to "PHP abstract properties"