common Posted October 12, 2010 Share Posted October 12, 2010 Hi I am still new to OOP PHP I have a question. Say I have: class class_name { function act_1() { } function act_2() { } } How can I call act_2() from the act_1() function? Because I want function act_1() to be private (meaning that it cannot be accessed by anything outside of this file) and that the code in act_1() can only be accessed when act_2() calls it. Or would it be better to use an extended class?? But how would that work? Thanks for any help! Quote Link to comment https://forums.phpfreaks.com/topic/215697-oop-php/ Share on other sites More sharing options...
rwwd Posted October 12, 2010 Share Posted October 12, 2010 $this->act_1() or $this->act_2() Respective to your function, $this just means referencing a function within your class. Private will work just means that you can't access that function outside the class, and just be aware that your using php5 class syntax, so this wont run on servers running php<5. Rw Quote Link to comment https://forums.phpfreaks.com/topic/215697-oop-php/#findComment-1121445 Share on other sites More sharing options...
Alex Posted October 12, 2010 Share Posted October 12, 2010 $this just means referencing a function within your class. Just to nitpick, $this references the object you're current in, and has nothing to do with whether you access a method, or property for that matter. and just be aware that your using php5 class syntax, so this wont run on servers running php<5. What PHP5 syntax is being used in that code? Quote Link to comment https://forums.phpfreaks.com/topic/215697-oop-php/#findComment-1121448 Share on other sites More sharing options...
rwwd Posted October 12, 2010 Share Posted October 12, 2010 Because I want function act_1() to be private Unless I have got this backwards and upside down; public; private; protected are php5 instructions, if that is wrong, I shall throw my book I got out of a magazine out of the window.. Which, judging by todays poor form on here, may be a good idea. From php.net In order to maintain backward compatibility with PHP 4, PHP 5 will still accept the use of the keyword var in property declarations instead of (or in addition to) public, protected, or private. However, var is no longer required. In versions of PHP from 5.0 to 5.1.3, the use of var was considered deprecated and would issue an E_STRICT warning, but since PHP 5.1.3 it is no longer deprecated and does not issue the warning. If you declare a property using var instead of one of public, protected, or private, then PHP 5 will treat the property as if it had been declared as public. If I read this right, am I wrong or right, I'm confused???!! I think I am correct... Rw Quote Link to comment https://forums.phpfreaks.com/topic/215697-oop-php/#findComment-1121521 Share on other sites More sharing options...
Alex Posted October 12, 2010 Share Posted October 12, 2010 There's a difference between having a method be private in concept, and it actually being defined as private as far as the PHP engine is concerned. In the OP, common shows no intention to make the method private using keywords in the code. That being said, PHP 5 is no new invention, it has been out for over 6 years and you shouldn't be using PHP 4 anymore. Having to code for cross-compatibly with PHP 4 at this point is pretty ridiculous. Quote Link to comment https://forums.phpfreaks.com/topic/215697-oop-php/#findComment-1121539 Share on other sites More sharing options...
MatthewJ Posted October 12, 2010 Share Posted October 12, 2010 In the OP, common shows no intention to make the method private using keywords in the code. but he does explicitly state that he wants it to be private Just sayin' Quote Link to comment https://forums.phpfreaks.com/topic/215697-oop-php/#findComment-1121548 Share on other sites More sharing options...
rwwd Posted October 12, 2010 Share Posted October 12, 2010 In the OP, common shows no intention to make the method private using keywords in the code. but he does explicitly state that he wants it to be private Just sayin' I wasn't imagining things then ;-p Anyway, thanks AlexWd for the advice, as for php4, there are a surprising number of people out there who still have php4.x.x running as their production/host servers, and in a few instances when asked to change to php5 they have refused, insisting on waiting for php6 to be made current release. To borrow the phrase from the previous poster; "Just sayin". At the moment I still check for php version, and then load the appropriate class file to run optimally with that version of php, its a headache, BUT it pay's dividends later on down the line... Rw Quote Link to comment https://forums.phpfreaks.com/topic/215697-oop-php/#findComment-1121555 Share on other sites More sharing options...
KevinM1 Posted October 12, 2010 Share Posted October 12, 2010 You did explain to them that PHP 6 won't be coming out, right? The old list of improvements will simply be rolled into PHP 5. AFAIK, the two biggest - namespaces and late static binding - have already been integrated. Quote Link to comment https://forums.phpfreaks.com/topic/215697-oop-php/#findComment-1121585 Share on other sites More sharing options...
trq Posted October 12, 2010 Share Posted October 12, 2010 There is also a huge push at the moment from all the big frameworks in particularly to move to 5.3. Most are going there and dropping backward compatibility with 5.2. Programming for php4 is ridiculous IMO. Your application design will suffer and your simply going to get left behind. Quote Link to comment https://forums.phpfreaks.com/topic/215697-oop-php/#findComment-1121587 Share on other sites More sharing options...
rwwd Posted October 12, 2010 Share Posted October 12, 2010 Programming for php4 is ridiculous IMO. Your application design will suffer and your simply going to get left behind. This isn't my usual way of doing things, my development setup is 5.3.0 and that's what I use for all my projects, I only go "backwards" if the situation dictates it, though I do moan at them, just like you would do! Clients eh! Rw Quote Link to comment https://forums.phpfreaks.com/topic/215697-oop-php/#findComment-1121588 Share on other sites More sharing options...
trq Posted October 12, 2010 Share Posted October 12, 2010 though I do moan at them, just like you would do! I wouldn't actually. Moaning will get you nowhere. I simply explain that it is imperative that systems be maintained with the latest software for the various security and feature reasons and then explain that I will not maintain an application on an old code base. Take it or leave it. Quote Link to comment https://forums.phpfreaks.com/topic/215697-oop-php/#findComment-1121599 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.