I'm building a big OOP Based PHP project, I've written 2 classes User and Page :
class User{
//user properties
private $_id,
$_name,
$_email;
//constructor
public function __construct(){}
//getters and setters
public function setId($id){
$this->$_id=$id;
}
public function setName($name){
$this->$_name=$name;
}
public function setId($email){
$this->$_email=$email;
}
public function getId(){
return $this->_id;
}
public function getName(){
return $this->_name;
}
public function getEmail(){
return $this->_email;
}
}
class Page{
//properties
private $_id,
$_title;
//constructor
public function __construct(){}
//getters and setters
public function setId($id){
$this->_id=$id;
}
public function setTitle($title){
$this->_title=$title
}
public function getId(){
return $this->_id;
}
public function getTitle(){
return $this->_title;
}
}
As you can conclude , I need to add another class DBManager containing add, edit, delete , list and views methods .
I don't know how to create a method (add for instance) in the class DBManager, that can add Users and Pages. without creating a class Manager for every class.
in other words , I want to create a class DBManager for all my objects.. instead of creating something like :
class User : class UserManager
class Page : class PageManager
Related
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();
404 error while executing code below. The Class 'MagazineModel' extends Controller like Class 'Magazine'. Qestion is: Cant i create object of 'MagazineModel' in Class 'Magazine'?
If i cant, how can i do that in different way? Thanks for help!
Class Magazine extends Controller
{
public $id;
public $name;
public $logo_path;
public $order;
public $description;
//model instance container
public $magazine_model;
public function __construct()
{
parent::__construct();
//properties from model
if (class_exists('MagazineModel')) {
$this->magazine_model = new MagazineModel();
}
}
public function getId()
{
return $this->id;
}
public function getName()
{
return $this->name;
}
public function getLogoPath()
{
return $this->logo_path;
}
public function getOrder()
{
return $this->order;
}
public function getDescription()
{
return $this->description;
}
public function testShow()
{
echo $this->twig->render('index.html.twig');
}
}
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);
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 */ }
}
?>