Jump to content

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/215697-oop-php/
Share on other sites

$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

 

 

Link to comment
https://forums.phpfreaks.com/topic/215697-oop-php/#findComment-1121445
Share on other sites

$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?

Link to comment
https://forums.phpfreaks.com/topic/215697-oop-php/#findComment-1121448
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/215697-oop-php/#findComment-1121521
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/215697-oop-php/#findComment-1121539
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/215697-oop-php/#findComment-1121555
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/215697-oop-php/#findComment-1121587
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/215697-oop-php/#findComment-1121588
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/215697-oop-php/#findComment-1121599
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.