Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #8205 > unrolled thread

classes in PHP

Started byRita Ferreira <ritaferreira83@gmail.com>
First post2013-02-03 09:04 -0800
Last post2013-02-04 09:15 +0200
Articles 13 — 6 participants

Back to article view | Back to comp.lang.php


Contents

  classes in PHP Rita Ferreira <ritaferreira83@gmail.com> - 2013-02-03 09:04 -0800
    Re: classes in PHP "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2013-02-03 18:11 +0100
    Re: classes in PHP Rita Ferreira <ritaferreira83@gmail.com> - 2013-02-03 09:49 -0800
      Re: classes in PHP "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2013-02-03 19:39 +0100
        Re: classes in PHP Jerry Stuckle <jstucklex@attglobal.net> - 2013-02-04 00:07 -0500
        Re: classes in PHP Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-02-06 13:29 +0100
          Re: classes in PHP Jerry Stuckle <jstucklex@attglobal.net> - 2013-02-06 08:49 -0500
          Re: classes in PHP "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2013-02-07 11:57 +0100
            Re: classes in PHP Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-02-07 12:17 +0100
            Re: classes in PHP Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-02-07 12:35 +0100
            Re: classes in PHP The Natural Philosopher <tnp@invalid.invalid> - 2013-02-07 11:46 +0000
              Re: classes in PHP "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2013-02-07 14:46 +0100
      Re: classes in PHP Simon <bad@example.com> - 2013-02-04 09:15 +0200

#8205 — classes in PHP

FromRita Ferreira <ritaferreira83@gmail.com>
Date2013-02-03 09:04 -0800
Subjectclasses in PHP
Message-ID<fd3565a9-3766-4224-bed5-812a0cd11d64@googlegroups.com>
Hi,
I'm new working with classes in PHP.

I'm trying to make login, and I have a form in html which the action I would like to go to the login function located in a class file called UserControler.php.
My form is in a php file but is not inside a class. I can't call the login function, it always give me error, so I tried to call a file called  process.php file where there I call the login function, but it gives me error:

It's something like this:

form.php

<form action="../process.php" method="post">
(..)

process.php

<?php
require("UserController.php");

$a=new UserController;
$a->login();
?>


UserController.php
<?php

public class UserController
{
	
	private $obj;

	function __construct( ){ }

	public function login()
	{}
(..)
}


Does anybody can tell me why my code only works until the "$a->login();" in process.php? Is anything wrong?

thanks for help!

[toc] | [next] | [standalone]


#8206

From"M. Strobel" <sorry_no_mail_here@nowhere.dee>
Date2013-02-03 18:11 +0100
Message-ID<an7k1cF1e41U1@mid.uni-berlin.de>
In reply to#8205
Am 03.02.2013 18:04, schrieb Rita Ferreira:
> Hi,
> I'm new working with classes in PHP.
> 
> I'm trying to make login, and I have a form in html which the action I would like to go to the login function located in a class file called UserControler.php.
> My form is in a php file but is not inside a class. I can't call the login function, it always give me error, so I tried to call a file called  process.php file where there I call the login function, but it gives me error:
> 
> It's something like this:
> 
> form.php
> 
> <form action="../process.php" method="post">
> (..)
> 
> process.php
> 
> <?php
> require("UserController.php");
> 
> $a=new UserController;
> $a->login();
> ?>
> 
> 
> UserController.php
> <?php
> 
> public class UserController

PHP Parse error:  syntax error, unexpected 'public' (T_PUBLIC) in php shell code on
line 1


> {
> 	
> 	private $obj;
> 
> 	function __construct( ){ }
> 
> 	public function login()
> 	{}
> (..)
> }
> 
> 
> Does anybody can tell me why my code only works until the "$a->login();" in process.php? Is anything wrong?

For development, always turn on every error display and logging directive you can find.

Learn how to check the web server log.

/Str.

[toc] | [prev] | [next] | [standalone]


#8208

FromRita Ferreira <ritaferreira83@gmail.com>
Date2013-02-03 09:49 -0800
Message-ID<c42bd135-6e8f-46e3-be37-445b423ff529@googlegroups.com>
In reply to#8205
Hi,
thanks for reply.
The error I'm obtained is: 

Fatal error: Class 'config' not found in /Applications/MAMP/htdocs/admin_classes/UserController.php on line 10

UserController.php

<?php

error_reporting(E_ALL);
ini_set('display_errors', '1');

class UserController
{
	
	private $obj;

	function __construct( ){}

	public function login()
	{
		if($_SERVER["REQUEST_METHOD"] == "POST")
		{
			$obj=new config();
			$obj->conectar();
                }
        (..)
        }
}

config.php

<?php

private $url_db;
private $login_db;
private $pass_db;
private $db;

class config
{

	function __construct( )
    { 
        session_start();
        $url_db="localhost";
        $login_db="root";
        $pass_db="root";
        $db="colmeia";
    }

private function conectar()
{
	mysql_connect($this->$url_db, $this->$login_db, $this->$pass_db);
    mysql_select_db($this->$db);
}
(..)
}

config.php is the file where I have all my queries/operations in DB.

Can you help me?
thanks

Domingo, 3 de Fevereiro de 2013 17:04:14 UTC, Rita Ferreira escreveu:
> Hi,
> 
> I'm new working with classes in PHP.
> 
> 
> 
> I'm trying to make login, and I have a form in html which the action I would like to go to the login function located in a class file called UserControler.php.
> 
> My form is in a php file but is not inside a class. I can't call the login function, it always give me error, so I tried to call a file called  process.php file where there I call the login function, but it gives me error:
> 
> 
> 
> It's something like this:
> 
> 
> 
> form.php
> 
> 
> 
> <form action="../process.php" method="post">
> 
> (..)
> 
> 
> 
> process.php
> 
> 
> 
> <?php
> 
> require("UserController.php");
> 
> 
> 
> $a=new UserController;
> 
> $a->login();
> 
> ?>
> 
> 
> 
> 
> 
> UserController.php
> 
> <?php
> 
> 
> 
> public class UserController
> 
> {
> 
> 	
> 
> 	private $obj;
> 
> 
> 
> 	function __construct( ){ }
> 
> 
> 
> 	public function login()
> 
> 	{}
> 
> (..)
> 
> }
> 
> 
> 
> 
> 
> Does anybody can tell me why my code only works until the "$a->login();" in process.php? Is anything wrong?
> 
> 
> 
> thanks for help!

[toc] | [prev] | [next] | [standalone]


#8209

From"M. Strobel" <sorry_no_mail_here@nowhere.dee>
Date2013-02-03 19:39 +0100
Message-ID<an7p7gF2ktvU1@mid.uni-berlin.de>
In reply to#8208
Am 03.02.2013 18:49, schrieb Rita Ferreira:
> Hi,
> thanks for reply.
> The error I'm obtained is: 
> 
> Fatal error: Class 'config' not found in /Applications/MAMP/htdocs/admin_classes/UserController.php on line 10
> 
> UserController.php
> 
> <?php
> 
> error_reporting(E_ALL);
> ini_set('display_errors', '1');
> 
> class UserController
> {
> 	
> 	private $obj;
> 
> 	function __construct( ){}
> 
> 	public function login()
> 	{
> 		if($_SERVER["REQUEST_METHOD"] == "POST")
> 		{
> 			$obj=new config();
> 			$obj->conectar();
>                 }
>         (..)
>         }
> }
> 
> config.php
> 
> <?php
> 
> private $url_db;
> private $login_db;
> private $pass_db;
> private $db;
> 
> class config
> {
> 
> 	function __construct( )
>     { 
>         session_start();
>         $url_db="localhost";
>         $login_db="root";
>         $pass_db="root";
>         $db="colmeia";
>     }

Please put your answer below the text. You are in the "usenet", read about it, and
use a news reader if you can.

The message says it all: class config is unknown. You must use
include/include_once/require/require_once to pull the definitions in.

You would never start a session somewhere inside the code, session_start() is the
first thing to do.

/Str.

[toc] | [prev] | [next] | [standalone]


#8226

FromJerry Stuckle <jstucklex@attglobal.net>
Date2013-02-04 00:07 -0500
Message-ID<kenfm0$4qa$1@dont-email.me>
In reply to#8209
On 2/3/2013 1:39 PM, M. Strobel wrote:
> Am 03.02.2013 18:49, schrieb Rita Ferreira:
>> Hi,
>> thanks for reply.
>> The error I'm obtained is:
>>
>> Fatal error: Class 'config' not found in /Applications/MAMP/htdocs/admin_classes/UserController.php on line 10
>>
>> UserController.php
>>
>> <?php
>>
>> error_reporting(E_ALL);
>> ini_set('display_errors', '1');
>>
>> class UserController
>> {
>> 	
>> 	private $obj;
>>
>> 	function __construct( ){}
>>
>> 	public function login()
>> 	{
>> 		if($_SERVER["REQUEST_METHOD"] == "POST")
>> 		{
>> 			$obj=new config();
>> 			$obj->conectar();
>>                  }
>>          (..)
>>          }
>> }
>>
>> config.php
>>
>> <?php
>>
>> private $url_db;
>> private $login_db;
>> private $pass_db;
>> private $db;
>>
>> class config
>> {
>>
>> 	function __construct( )
>>      {
>>          session_start();
>>          $url_db="localhost";
>>          $login_db="root";
>>          $pass_db="root";
>>          $db="colmeia";
>>      }
>
> Please put your answer below the text. You are in the "usenet", read about it, and
> use a news reader if you can.
>
> The message says it all: class config is unknown. You must use
> include/include_once/require/require_once to pull the definitions in.
>
> You would never start a session somewhere inside the code, session_start() is the
> first thing to do.
>
> /Str.
>

Almost... If you have a class object in the session, you need to include 
or require the class object first.

Generally, in require_once() the necessary class code, *then* call 
session_start().


-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#8311

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-02-06 13:29 +0100
Message-ID<2693767.JdZqatdWKk@PointedEars.de>
In reply to#8209
M. Strobel wrote:
^^
Please use your full name.  That, too, is customary in Usenet; not only 
because it reduces the potential for confusion with other people around the 
world that have the same initials, but also because it is considered polite 
to introduce oneself by name to strangers before further communication.

> Am 03.02.2013 18:49, schrieb Rita Ferreira:
>> The error I'm obtained is:
>> 
>> Fatal error: Class 'config' not found in
>> /Applications/MAMP/htdocs/admin_classes/UserController.php on line 10
>> 
>> UserController.php
>> 
>> <?php
>> 
>> error_reporting(E_ALL);
>> ini_set('display_errors', '1');
>> 
>> class UserController
>> {
>> 
>> private $obj;
>> 
>> function __construct( ){}
>> 
>> public function login()
>> {
>> if($_SERVER["REQUEST_METHOD"] == "POST")
>> {
>> $obj=new config();
>> $obj->conectar();
>>                 }
>>         (..)
>>         }
>> }
>> 
>> config.php
>> 
>> <?php
>> 
>> private $url_db;
>> private $login_db;
>> private $pass_db;
>> private $db;
>> 
>> class config
>> {
>> 
>> function __construct( )
>>     { 
>>         session_start();
>>         $url_db="localhost";
>>         $login_db="root";
>>         $pass_db="root";
>>         $db="colmeia";
>>     }
> 
> Please put your answer below the text. You are in the "usenet", read about
> it, and use a news reader if you can.

To be precise, one should put the answer below each *section* of quoted 
text.  The parts that are not relevant to the answer should not be quoted at 
all.  That is, usually greetings and thanks should not be quoted.

That is why a NetNews agent usually places the cursor above the quote (works 
as designed), so that you can go down to the bottom while removing the parts 
that you do not refer to and answering the parts that you do refer to.  Some 
people mistake this behavior, sometimes because of (bad) business practice, 
as indication that top-posting would be wanted when it fact there are few 
things more disturbing than top-posting in a discussion (people are usually 
not bats hanging from the ceiling, after all).

<http://www.netmeister.org/news/learn2quote.html>
 
> The message says it all: class config is unknown. You must use
> include/include_once/require/require_once to pull the definitions in.

Not necessarily, and “require_once” is recommended instead of “require” for 
loading class files.  For there are autoloaders in PHP, particularly useful 
in an Model-View-Controller (MVC) approach (as this one appears to be one):

<http://pear.php.net/manual/en/standards.including.php>
<http://php.net/manual/en/language.oop5.autoload.php>
<http://pear.php.net/pepr/pepr-proposal-show.php?id=634>
 
> You would never start a session somewhere inside the code, session_start()
> is the first thing to do.

That is what they are doing, but only with POST requests and (one can 
surmise from the identifiers) on user login.  That session_start() is called 
in a constructor called from a method of a controller (see: MVC) here does 
not change it.  If this code worked, the call stack would be:

  start_session()
  config::__construct()
  UserController::login()

That is *sound* object-oriented PHP programming except for the fact that the 
“private” keyword is not allowed outside of a “class” declaration, and that 
private properties would need to be referred to with “$this->url_db” etc.

Occam's razor suggests that the OP might have been looking for package-
private variables, but there is no such thing in PHP; you have to use class 
variables which are static properties of the respective class object.  In 
this case, however, dynamic properties appear to be the better approach, 
because the “config” class (the class name should be “Config” instead) is 
instantiated.

This code most certainly breaks because of the syntax error in config.php 
(which should be “Config.php” then).  There should be an error message like 
the following before the one posted here:

$ php -r 'private $x;'
PHP Parse error:  syntax error, unexpected T_PRIVATE in Command line code on 
line 1

$ php -v
PHP 5.3.3-7+squeeze14 with Suhosin-Patch (cli) (built: Aug  6 2012 20:08:59) 
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
    with XCache v1.3.0, Copyright (c) 2005-2009, by mOo
    with Xdebug v2.1.0, Copyright (c) 2002-2010, by Derick Rethans
    with Suhosin v0.9.32.1, Copyright (c) 2007-2010, by SektionEins GmbH


See also: <http://php.net/manual/en/language.oop5.visibility.php>


PointedEars
-- 
var bugRiddenCrashPronePieceOfJunk = (
    navigator.userAgent.indexOf('MSIE 5') != -1
    && navigator.userAgent.indexOf('Mac') != -1
)  // Plone, register_function.js:16

[toc] | [prev] | [next] | [standalone]


#8312

FromJerry Stuckle <jstucklex@attglobal.net>
Date2013-02-06 08:49 -0500
Message-ID<ketn0m$24i$1@dont-email.me>
In reply to#8311
On 2/6/2013 7:29 AM, Thomas 'PointedEars' Lahn wrote:
> M. Strobel wrote:
> ^^
> Please use your full name.  That, too, is customary in Usenet; not only
> because it reduces the potential for confusion with other people around the
> world that have the same initials, but also because it is considered polite
> to introduce oneself by name to strangers before further communication.
>

Here's a better idea.  Why don't you take your Pointed Head and stick it 
where the sun doesn't shine instead of trying to get the rest of the 
world to conform to YOUR idea of what usenet should be?

There is no rule that people use their full name.  And maybe if you 
would take a manners class you could find out what "polite" really 
means.  It does NOT mean constantly correcting people for YOUR perceived 
breach of manners!

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#8320

From"M. Strobel" <sorry_no_mail_here@nowhere.dee>
Date2013-02-07 11:57 +0100
Message-ID<anhfjsF8optU1@mid.uni-berlin.de>
In reply to#8311
On 06.02.2013 13:29, Thomas 'PointedEars' Lahn wrote:
> M. Strobel wrote:
> ^^
> Please use your full name.  That, too, is customary in Usenet; not only 
> because it reduces the potential for confusion with other people around the 
> world that have the same initials, but also because it is considered polite 
> to introduce oneself by name to strangers before further communication.
> 
> PointedEars
> 

In the eighties you made shake hands with every internet user.

Today we have the concept of the "right to use a pseudonym", especially here in
Germany, while almost everywhere else people seem to bother less about privacy.

see https://www.datenschutzzentrum.de/ (german)

/Str.
P.S. M. Strobel is not even a pseudonym

[toc] | [prev] | [next] | [standalone]


#8321

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-02-07 12:17 +0100
Message-ID<90688054.0BqJMQ3gyG@PointedEars.de>
In reply to#8320
M. Strobel wrote:

> On 06.02.2013 13:29, Thomas 'PointedEars' Lahn wrote:
>> M. Strobel wrote:
>> ^^
>> Please use your full name.  That, too, is customary in Usenet; not only
>> because it reduces the potential for confusion with other people around
>> the world that have the same initials, but also because it is considered
>> polite to introduce oneself by name to strangers before further
>> communication.
> 
> In the eighties you made shake hands with every internet user.
> 
> Today we have the concept of the "right to use a pseudonym", especially
> here in Germany, while almost everywhere else people seem to bother less
> about privacy.
> 
> see https://www.datenschutzzentrum.de/ (german)

How ridiculous of you to cite German Federal law to me of all people, and to 
do that in an international newsgroup.  German Federal law prevents your 
service provider from not accepting you because of your wish to use a 
pseudonym (“Informationelle Selbstbestimmung”).  And “Datenschutz” (data 
protection) is a completely different thing that you think it is.

You have the right to be impolite like that.  I have the right to hint you 
on or ignore you for your impoliteness, the latter leaving you with your 
misconceptions about PHP.

See also: <http://www.kirchwitz.de/~amk/dni/netiquette>, #14

> M. Strobel is not even a pseudonym

Which makes your argument even more ridiculous.


Score adjusted, F'up2 poster

PointedEars
-- 
> If you get a bunch of authors […] that state the same "best practices"
> in any programming language, then you can bet who is wrong or right...
Not with javascript. Nonsense propagates like wildfire in this field.
  -- Richard Cornford, comp.lang.javascript, 2011-11-14

[toc] | [prev] | [next] | [standalone]


#8322

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-02-07 12:35 +0100
Message-ID<8954644.YvmPAdJ5Ed@PointedEars.de>
In reply to#8320
M. Strobel wrote:

> On 06.02.2013 13:29, Thomas 'PointedEars' Lahn wrote:
>> M. Strobel wrote:
>> ^^
>> Please use your full name.  That, too, is customary in Usenet; not only
>> because it reduces the potential for confusion with other people around
>> the world that have the same initials, but also because it is considered
>> polite to introduce oneself by name to strangers before further
>> communication.
> 
> In the eighties you made shake hands with every internet user.
> 
> Today we have the concept of the "right to use a pseudonym", especially
> here in Germany, while almost everywhere else people seem to bother less
> about privacy.
> 
> see https://www.datenschutzzentrum.de/ (german)

Lest I forget, since you are so keen to refer to your (assumed) rights:

You do _not_ have the right to put that pseudo-address into your From header 
field value, as it is violating the Acceptable Use Policy of your service 
provider.  Continuing to do so can lead to termination of your account:

<http://www.zedat.fu-berlin.de/NetNews-Regeln>


F'up2 poster

PointedEars
-- 
    realism:    HTML 4.01 Strict
    evangelism: XHTML 1.0 Strict
    madness:    XHTML 1.1 as application/xhtml+xml
                                                    -- Bjoern Hoehrmann

[toc] | [prev] | [next] | [standalone]


#8323

FromThe Natural Philosopher <tnp@invalid.invalid>
Date2013-02-07 11:46 +0000
Message-ID<kf0479$o6v$2@news.albasani.net>
In reply to#8320
On 07/02/13 10:57, M. Strobel wrote:
> On 06.02.2013 13:29, Thomas 'PointedEars' Lahn wrote:
>> M. Strobel wrote:
>> ^^
>> Please use your full name.  That, too, is customary in Usenet; not only
>> because it reduces the potential for confusion with other people around the
>> world that have the same initials, but also because it is considered polite
>> to introduce oneself by name to strangers before further communication.
>>
>> PointedEars
>>
>
> In the eighties you made shake hands with every internet user.
>
in the eighties most people on the internet were people you wanted to 
know, and who probably wanted to know you..



-- 
Ineptocracy

(in-ep-toc’-ra-cy) – a system of government where the least capable to 
lead are elected by the least capable of producing, and where the 
members of society least likely to sustain themselves or succeed, are 
rewarded with goods and services paid for by the confiscated wealth of a 
diminishing number of producers.

[toc] | [prev] | [next] | [standalone]


#8324

From"M. Strobel" <sorry_no_mail_here@nowhere.dee>
Date2013-02-07 14:46 +0100
Message-ID<anhphfFb1lhU1@mid.uni-berlin.de>
In reply to#8323
On 07.02.2013 12:46, The Natural Philosopher wrote:
> On 07/02/13 10:57, M. Strobel wrote:
>> On 06.02.2013 13:29, Thomas 'PointedEars' Lahn wrote:
>>> M. Strobel wrote:
>>> ^^
>>> Please use your full name.  That, too, is customary in Usenet; not only
>>> because it reduces the potential for confusion with other people around the
>>> world that have the same initials, but also because it is considered polite
>>> to introduce oneself by name to strangers before further communication.
>>>
>>> PointedEars
>>>
>>
>> In the eighties you made shake hands with every internet user.
>>
> in the eighties most people on the internet were people you wanted to know, and who
> probably wanted to know you..

So true. And leaves room for interpretation as to who wants to know whom today...

/Str.

[toc] | [prev] | [next] | [standalone]


#8227

FromSimon <bad@example.com>
Date2013-02-04 09:15 +0200
Message-ID<an95goFbqvhU1@mid.individual.net>
In reply to#8208
On 2013/02/03 07:49 PM, Rita Ferreira wrote:

>
> class UserController
> {
> 	
> 	private $obj;
>
> 	function __construct( ){}
>
> 	public function login()
> 	{
> 		if($_SERVER["REQUEST_METHOD"] == "POST")
> 		{
> 			$obj=new config();
> 			$obj->conectar();
>                  }

...

>
> config.php
>
> <?php
>
> private $url_db;
> private $login_db;
> private $pass_db;
> private $db;
>
> class config
> {
>
> 	function __construct( )
>      {
>          session_start();
>          $url_db="localhost";
>          $login_db="root";
>          $pass_db="root";
>          $db="colmeia";
>      }
>
> private function conectar()

...

>

Further to what was said already, the function 'conectar' is private and 
cannot be accessed.

Simon


[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web