473,763 Members | 8,423 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

auto construct parent class

Hello,

I have an AppController and some classes that extend the controller.

I.e:
class UsersController extends AppController
{
function UsersController ()
{
parent::AppCont roller();
}

.....
}

1) Is it the same using function UsersController () {} and function
_construct() {} ??

2) Is there a way not to have in every controller the
function UsersController () {parent::AppCon troller();}
Or in other way, is it possible for the parent construction function to
be executed automatically?

thanks
Oct 22 '08 #1
2 3217
Harris Kosmidhs wrote:
Hello,

I have an AppController and some classes that extend the controller.

I.e:
class UsersController extends AppController
{
function UsersController ()
{
parent::AppCont roller();
}

.....
}

1) Is it the same using function UsersController () {} and function
_construct() {} ??
Yep
2) Is there a way not to have in every controller the
function UsersController () {parent::AppCon troller();}
Or in other way, is it possible for the parent construction function to
be executed automatically?
Iirc Not,

Child constructor overrides the parent constructor making it not
construct :-)

You could do something smart with autoload or whater u use but the
constructor will only be called once.

function __autoload($cls ){
require_once(PA TH_CLS.$cls.'.c ls.php');
if(method_exist s($class_name,' my_construct')) {
call_user_func( array($cls,'my_ construct'));
}
}
Floortje

--
Currently working on dogcetera.com
Oct 22 '08 #2
On Wed, 22 Oct 2008 11:04:09 +0200, Harris Kosmidhs
<hk******@remov e.me.softnet.tu c.grwrote:
Hello,

I have an AppController and some classes that extend the controller.

I.e:
class UsersController extends AppController
{
function UsersController ()
{
parent::AppCont roller();
}

.....
}

1) Is it the same using function UsersController () {} and function
_construct() {} ??
__construct(), but yeah, for all practical purposes they are the same.
2) Is there a way not to have in every controller the
function UsersController () {parent::AppCon troller();}
Or in other way, is it possible for the parent construction function to
be executed automatically?
1) As long as you're using PHP5 (PHP4 has been EOL for a while...), just
use __construct(), it will make your live easier.
2) Although the function name is different, the parent constructor will be
called if you haven't defined a constructor in your child class, without
you having to specify it. For example:
<?php
class Foo{
function Foo(){
echo __CLASS__.':'._ _FUNCTION__.' called';
}
}
class Bar extends Foo{}
new Bar();
?>
Output:
Foo:Foo called

3) If you override the constructor, there is no other way to run the
parents constructor save for putting it explicitly (i.e.
parent::__const ruct()) in your new constructor.

4) Often, when a constructor performs a lot of statement, people choose to
move either the code that differs (or the code that remains the same) to
another function (init() for example), which get's called from the
constructor, which you can override or keep as you wish rather then extend
the constructor. In my opinion that's largely a matter of taste.
--
Rik Wasmus
Oct 22 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
2225
by: nenad | last post by:
Wouldn't it be nice if we could do something like this: class Funky{ public: auto virtual void doStuff(){ // dostuff } };
28
14259
by: PerryC | last post by:
Anyone know how to auto close the parent / opener window without confirmation? I have tried: <script> opener.window.close() </script> ----I put it in the child html page, and nothing happen!!---
3
2601
by: Chris | last post by:
Before I started to create table, etc to track unique form field record number assigments I thought I'd check to see if there is now a better way to do this in .NET. I have a parent form (table) and children form (table). Relationship equals one to many. I'd like to auto number the fields accordingly and traditionaly I assign a unique number based on a table value that I retrieve + 1. i.e. Parent record field value = 1 Children record...
0
1027
by: jean-michel bain-cornu | last post by:
Hi all, I'm trying to get the whole field selected when the caret arrives into a wx masked control. This is perfectly done with the program below. But if I comment out the line "r= dlg.ShowModal()", I get something different : the data within the field is no more correctly selected. Why ? And is there any solution ? If somebody have got an idea, I'd be delighted.
6
6081
by: reandeau | last post by:
I'm building out a OO based app in PHP 5 but I'm getting a little confused on children contructing parents. I have a parent that looks like this: abstract Class State { protected $database; protected $user; protected $output; public function __construct($database,$user,$output) { $this->database = $database;
28
1884
by: Ilias Lazaridis | last post by:
I understand that I can use __metaclass__ to create a class which modifies the behaviour of another class. How can I add this metaclass to *all* classes in the system? (In ruby I would alter the "Class" class) .. http://lazaridis.com
4
41711
by: Harris Kosmidhs | last post by:
Consider the following HTML: <div class="links"> <img src="linkimages/logo.gif"> <h2>title</h2> <span><a target="_blank" href="http://">http://</a></span> <p>description of link</p> </div> I have the following CSS:
5
3484
by: iKiLL | last post by:
Hi All, I am trying to Bulid Windows Mobile Forms Control with C# in VS2005 using CF2. On this control A lable is created and some text set for the control. My problem is that i dont know how lond the text is going to be. so i need to set the height and width of the label a cording to the data. un fortunatly i have not been able to find a simple Auto Resize property
1
3747
by: bruce628 | last post by:
I want to use SWT Label and popmenu to construct a menubar ,and the effect of this menubar is same to the menubar in SWT.When click the Label,it should be highlighted and popmenu shows.The issue is when click the label and move the mouse to enter next label,the next label can not be highlighted.Can anyone have a solution for it? Here is my code: import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.*; public class labelmenu { ...
0
9387
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10148
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10002
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9938
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9823
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8822
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6643
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2794
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.