in php I have this code. I'm trying to get an inherited method to utilize a member variable of its child class.
abstract class HtmlObj{
//abstract protected function jQuery_Activity();
public $hyperlink;
abstract protected function php_Activity();
abstract protected function print_Widget();
function __construct($hyperlink=""){
if(isset($hyperlink)){
$this->hyperlink = $hyperlink;
}
$this->php_Activity();
$this->Print_Widget();
}
}
class child extends HtmlObj{
public $id;
protected function php_Activity(){return;}
protected function print_Widget(){
print $this->id;
}
function __construct($id){
this->id = $id;
}
}
unfortunately this prints nothing. any insights as to why?
in child class You need to reffer to parent::__construct() by doing something like
abstract class HtmlObj
{
//abstract protected function jQuery_Activity();
public $hyperlink;
abstract protected function php_Activity();
abstract protected function print_Widget();
function __construct($hyperlink = "")
{
if (isset($hyperlink)) {
$this->hyperlink = $hyperlink;
}
$this->php_Activity();
$this->Print_Widget();
}
}
class child extends HtmlObj
{
public $id;
protected function php_Activity()
{
return;
}
protected function print_Widget()
{
print $this->id;
}
function __construct($id)
{
$this->id = $id;
parent::__construct();
}
}
new child(10);
Related
In the application I'm working on, the Model part of the MVC stack is designed to work trough singletons; each Model has a __getInstanceMethod which is
protected static $singleton;
public static function __getInstance(): self {
if(self::$singleton === null) {
self::$singleton = __CLASS__;
self::$singleton = new self::$singleton;
}
return self::$singleton;
}
End result is, if __getInstance() is called twice on the same Model class, it returns the same exact object both times.
I tried to reduce code duplication by moving the __getInstance() method to the Model's parent class, BaseModel, by editing it like so.
class BaseModel {
protected static $singleton;
public static function __getInstance(): self {
if (static::$singleton === null) {
static::$singleton = static::class;
static::$singleton = new static::$singleton();
}
return static::$singleton;
}
}
class AModel extends BaseModel {
protected static $singleton;
/** ... */
}
class BModel extends BaseModel {
protected static $singleton;
/** ... */
}
AModel::__getInstance(); // AModel
BModel::__getInstance(); // BModel
Problem is, I need to manually add a $singleton property to each and every Model class, otherwise I'll always get returned the instance of the first Model class I called the method on.
class BaseModel {
protected static $singleton;
public static function __getInstance(): self {
if (static::$singleton === null) {
static::$singleton = static::$class;
static::$singleton = new static::$singleton();
}
return static::$singleton;
}
}
class AModel extends BaseModel {}
class BModel extends BaseModel {}
AModel::__getInstance(); // AModel
BModel::__getInstance(); // Still AModel
Is there a way I can avoid doing that?
You could switch to an "instance map", e.g.:
<?php
declare(strict_types=1);
error_reporting(-1);
ini_set('display_errors', 'On');
class BaseModel
{
protected static $instances = [];
public static function __getInstance(): self
{
if (!isset(static::$instances[static::class])) {
static::$instances[static::class] = new static();
}
return static::$instances[static::class];
}
}
class AModel extends BaseModel
{
}
class BModel extends BaseModel
{
}
echo get_class(AModel::__getInstance()), "\n";
echo get_class(BModel::__getInstance());
https://3v4l.org/qG0qJ
and with 7.4+ it could be simplified to:
<?php
declare(strict_types=1);
error_reporting(-1);
ini_set('display_errors', 'On');
class BaseModel
{
private static array $instances = [];
public static function __getInstance(): self
{
return static::$instances[static::class] ??= new static();
}
}
I am creating the dynamic crud, but I want the casting method (database array to object of current class) to be inherited directly to the children I need to get the name the reference this but to the object that I inherited the method, it works perfect if I put it in the class inherited but if I have 10 thousand inherited classes I have to copy 10 thousand times that (that's what I want to avoid)
<?php
namespace Models;
use Interfaces\ICrud;
use db\DBconection;
require('./interfaces/icrud.php');
require('./db/dbconnect.php');
abstract class Model implements ICrud{
private $connection;
protected $schema='public.';
public function __construct(){
}
public function get($id){
return (new DBconection())->runQuery("SELECT * from $this->schema".$this->getModelName(get_class($this))." WHERE id=$id");
}
public function create($entity){
}
public function update($entity){
}
public function delete($id){
return (new DBconection())->runQuery("DELETE from $this->schema".$this->getModelName(get_class($this))." WHERE id=$id");
}
private function getModelName($class){
return strtolower(str_replace('Model','',(new \ReflectionClass($class))->getShortName()));
}
}
this is the child class
<?php
namespace Models;
use Models\Model,
db\DBconection;
require('model.php');
class UsersModel extends Model {
private $id;
private $username;
private $password;
private $active;
private $create;
private $update;
private $id_typeuser;
private $id_people;
protected $schema='security.';
public function __construct(){
}
}
Given I have an abstract class:
abstract class User{
private function getNumber(){
return 'Get number';
}
}
With the child class
class UserComments extends User{
public function rows(){
return $this->getNumberOfRows();
}
}
Question:
Is there any way to call the getNumber function when I try to call getNumberOfRows method in child class, or when I call getNumberOfRows() in child class I want getNumber to be called instead
?
Due to PHP's Object inheritance you can directly call the specific method. However, in your example the getNumberOfRows() function is missing.
class UserComments extends User {
public function rows() {
return $this->getNumber();
}
}
You can do something like this
abstract class User{
private function getNumber(){
return 'Get number';
}
public function getNumberOfRows(){
return $this->getNumber();
}
}
With the child class
class UserComments extends User{
public function rows(){
return $this->getNumberOfRows(); //Defined in the parent class
}
}
Abstract methods cannot be private, abstract must be implemented by the class that derived it.
You can either use public, or if you do not want it to be visible outside, make it protected, as following:
abstract class User{
abstract protected function getNumber();
}
Once you do this, you can implement the getNumber method in User class:
class User {
protected function getNumber() {
// Do something
}
}
Update: Please note that protected methods are accessible by child classes, you can use "hierarchy":
abstract class User{
abstract protected function getNumber();
}
class UserComment extends User {
protected function comment() {
// Do something
}
protected function getNumber() {
return 3;
}
}
class Post extends UserComment {
public function myMethod() {
echo $this->getNumber();
}
}
Also you can use interfaces, just an example:
interface User {
public function getNumber();
}
class UserComment {
protected function myMethod() {
// Do something
}
}
class Post extends UserComment implements User {
final public function getNumber() {
return 3;
}
public function myMethod() {
echo $this->getNumber();
}
}
$post = new Post();
$post->myMethod();
Is it OK to use properties/methods from parent classes in trait methods?
This code works, but is it good practice?
class Child extends Base{
use ExampleTrait;
public function __construct(){
parent::__construct();
}
public function someMethod(){
traitMethod();
}
}
trait ExampleTrait{
protected function traitMethod(){
// Uses $this->model from Base class
$this->model->doSomething();
}
}
I don't think it's good practice.
Instead you could have a method to fetch your model object, and have that method as an abstract signature in you trait:
trait ExampleTrait {
abstract protected function _getModel();
protected function traitMethod() {
$this->_getModel()->doSomething();
}
}
class Base {
protected $_model;
protected function _getModel() {
return $this->_model;
}
}
class Child extends Base {
use ExampleTrait;
public function someMethod() {
$this->traitMethod();
}
}
Or pass your model as a parameter to your trait method:
trait ExampleTrait {
protected function traitMethod($model) {
$model->doSomething();
}
}
class Base {
protected $_model;
}
class Child extends Base {
use ExampleTrait;
public function someMethod() {
$this->traitMethod($this->_model);
}
}
Both of these approaches let you utilize your IDE's type hinting.
I have a PHP object with an id that should not be changed, except in one specific context, when a specific method is called. But this method is outside the object class and can't inherit. I don't know what's the better way to do this properly, how can I "check" if the object setter is called by the right method, to avoid abuses?
Thanks in advance for your help
edit: here is an example:
<?php
class myClass {
private $id;
private $var1;
private $var2;
private function setId($id){
//this must be accessible only when calling "secureSetId()" from class "secureClass"
$this->id = $id;
}
public function setVar1{
//this is a public function...
}
...
}
class secureClass {
private function secureSetId($id){
//this is accessible only here, but how to call private function "setId" from class "myClass"?
}
...
}
?>
<?php
interface SimpleMyClass {
public function setVar1($value);
public function setVar2($value);
}
interface SecureMyClass extends SimpleMyClass {
public function setId($id);
}
class MyClass implements SecureMyClass {
private $id;
private $var1;
private $var2;
public function setVar1($value) { /* ... */ }
public function setVar2($value) { /* ... */ }
public function setId($id) { /* setting id */ }
}
// classes that use "different versions" of MyClass
class SecureOtherClass {
public function setMyClass(SecureMyClass $my) { /* this class is able to set id of MyClass */ }
}
class SimpleOtherClass {
public function setMyClass(SimpleMyClass $my) { /* this class uses simple version of MyClass */ }
}
?>