Tagged Questions
147
votes
5answers
19k views
How should a model be structured in MVC?
I am just getting a grasp on the MVC framework and often I wonder how much code should go in the model. I trend of have a data access class that has methods like this:
public function ...
56
votes
9answers
30k views
What is the best method to merge two PHP objects?
We have two PHP5 objects and would like to merge the content of one into the second. There are no notion of subclasses between them so the solutions described in the following topic cannot apply.
How ...
50
votes
9answers
16k views
What is the point of interfaces in PHP?
Interfaces allow you to create code which defines the methods of classes that implement it. You cannot however add any code to those methods.
Abstract classes allow you to do the same thing, along ...
48
votes
11answers
8k views
Who needs singletons? [closed]
Imagine you access your MySQL database via PDO. You got some functions, and in these functions, you need to access the database.
The first thing I thought of is global, like:
$db = new ...
48
votes
11answers
41k views
Multiple Inheritance in PHP
I'm looking for a good, clean way to go around the fact that PHP5 still doesn't support multiple inheritance. Here's the class hierarchy:
Message
-- TextMessage
-------- InvitationTextMessage
...
47
votes
9answers
29k views
Getter and Setter?
I'm not a PHP developer, so I'm wondering if in PHP is more popular to use explicit getter/setters, in a pure OOP style, with private fields (the way I like):
class MyClass {
private $firstField;
...
46
votes
6answers
9k views
Interface or an Abstract Class: which one to use?
Please explain when I should use an interface and when I should use abstract class?
How I can change my abstract class in to an interface?
44
votes
5answers
3k views
If Singletons are bad then why is a Service Container good?
We all know how bad Singletons are because they hide dependencies and for other reasons.
But in a framework, there could be many objects that need to be instantiated only once and called from ...
43
votes
5answers
18k views
Why does PHP 5.2+ disallow abstract static class methods?
After enabling strict warnings in PHP 5.2, I saw a load of strict standards warnings from a project that was originally written without strict warnings:
Strict Standards: Static function ...
42
votes
9answers
8k views
When to use static vs instantiated classes
PHP is my first programming language. I can't quite wrap my head around when to use static classes vs instantiated objects.
I realize that you can duplicate and clone objects. However in all of my ...
40
votes
12answers
29k views
Can you create instance properties dynamically in PHP?
Is there any way to create all instance properties dynamically? For example, I would like to be able to generate all attributes in the constructor and still be able to access them after the class is ...
39
votes
12answers
2k views
Why is the amount of visibility on methods and attributes important?
Why shouldn't one leave all methods and attributes accessible from anywhere (i.e. public)?
Can you give me an example of a problem I can run into if I declared an attribute as public?
39
votes
2answers
8k views
What does it mean to start a PHP function with an ampersand?
I'm using the Facebook library with this code in it:
class FacebookRestClient {
...
public function &users_hasAppPermission($ext_perm, $uid=null) {
return ...
36
votes
22answers
16k views
Which PHP CMS has the best architecture? [closed]
In your opinion, which PHP CMS has the best architecture? I don't care about how easy its admin panel is to use, or how many CMS features it has. Right now, I'm after how good its code is (so, ...
34
votes
3answers
6k views
ACL implementation
First question
Please, could you explain me how simpliest ACL could be implemented in MVC.
Here is the first approach of using Acl in Controller...
<?php
class MyController extends Controller {
...