Tag Archives: struct

CakePHP ACL Database Setup: ARO / ACO structure?

Questions: I’m struggling to implement ACL in CakePHP. After reading the documentation in the cake manual as well as several other tutorials, blog posts etc, I found Aran Johnson’s excellent tutorial which has helped fill in many of the gaps. His examples seem to conflict with others I’ve seen though in a few places –… Read More »

Php dynamic class construction

Questions: I’m trying to avoid the use of eval. I can dynamically instantiate a class like this: class myclass {} $my_class_name = ‘myclass’; $obj = new $myclass(); If the constructor is like follows: class myclass { public function __construct( $argument1, $argument2 ) {} } and i have the values of the arguments in an array,… Read More »

Benefits of using a constructor?

Questions: In my quest in trying to learn more about OOP in PHP. I have come across the constructor function a good few times and simply can’t ignore it anymore. In my understanding, the constructor is called upon the moment I create an object, is this correct? But why would I need to create this… Read More »

PDO using PDO::FETCH_PROPS_LATE and __construct() call?

Questions: I’m trying to create a new instance of Setting object calling __construct() method with PHP PDO and constrain PDO::FETCH_PROPS_LATE. Unfortunatly i’m getting this warning (and binding doesn’t work). How can pass column values to the constructor method? Warning: Missing argument 1 for Setting::__construct() in pdo.php. Notice: Undefined variable: key in pdo.php. class Setting {… Read More »

Setting variables on Constructor VS on the class definition

Questions: Lately I’ve been wondering if there’s a difference between initializing the variables that have a default value on the Constructor VS on the class definition. Which one is better, taking into consideration the optimization: class TestClass { private $test_var = ‘Default Value’; function __construct() { } } class TestClass2 { private $test_var; function __construct()… Read More »

How to call Entity Manager in a constructor?

Questions: I’ve been trying to call Entity Manager in a constructor: function __construct() { $this->getDoctrine()->getEntityManager(); … but, as I’ve seen in this answer: Stackoverflow question, it can’t be done. So I wonder if there is a way to achieve it, as I have to call it often, and want to do some stuff in the… Read More »

How do I get the type of constructor parameter via reflection?

Questions: I’m using type hinting on my constructor parameter list like so: public function __construct(FooRepository $repository) Is there a way to use the PHP Reflection API to get the hinted type? In other words, I want a reflection function that I can call to somehow get back the string “FooRepository”. I’ve tried getting the constructor… Read More »

Call parent constructor before child constructor in PHP

Questions: I was wondering if its possible to call the parents __construct(), before the child’s __construct() with inheritance in PHP. Example: class Tag { __construct() { // Called first. } } class Form extends Tag { __construct() { // Called second. } } new Form(); Ideally, I would be able to do something in between… Read More »

Why not “instantiate a new object inside object constructor”?

Questions: I answered a question (link) that I used a creation of the new object in another class’ constructor, here the example: class Person { public $mother_language; function __construct(){ // just to initialize $mother_language $this->mother_language = new Language(‘English’); } And I got the comment from user “Matija” (his profile) and he wrote: You should never… Read More »