FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » classes in PHP
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
classes in PHP [message #180271] Sun, 03 February 2013 17:04 Go to next message
Rita Ferreira is currently offline  Rita Ferreira
Messages: 4
Registered: February 2013
Karma: 0
Junior Member
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!
Re: classes in PHP [message #180272 is a reply to message #180271] Sun, 03 February 2013 17:11 Go to previous messageGo to next message
M. Strobel is currently offline  M. Strobel
Messages: 386
Registered: December 2011
Karma: 0
Senior Member
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.
Re: classes in PHP [message #180274 is a reply to message #180271] Sun, 03 February 2013 17:49 Go to previous messageGo to next message
Rita Ferreira is currently offline  Rita Ferreira
Messages: 4
Registered: February 2013
Karma: 0
Junior Member
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!
Re: classes in PHP [message #180275 is a reply to message #180274] Sun, 03 February 2013 18:39 Go to previous messageGo to next message
M. Strobel is currently offline  M. Strobel
Messages: 386
Registered: December 2011
Karma: 0
Senior Member
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.
Re: classes in PHP [message #180292 is a reply to message #180275] Mon, 04 February 2013 05:07 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
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(at)attglobal(dot)net
==================
Re: classes in PHP [message #180293 is a reply to message #180274] Mon, 04 February 2013 07:15 Go to previous messageGo to next message
Simon is currently offline  Simon
Messages: 29
Registered: February 2011
Karma: 0
Junior Member
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
Re: classes in PHP [message #180377 is a reply to message #180275] Wed, 06 February 2013 12:29 Go to previous messageGo to next message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
Senior Member
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
Re: classes in PHP [message #180378 is a reply to message #180377] Wed, 06 February 2013 13:49 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
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(at)attglobal(dot)net
==================
Re: classes in PHP [message #180386 is a reply to message #180377] Thu, 07 February 2013 10:57 Go to previous messageGo to next message
M. Strobel is currently offline  M. Strobel
Messages: 386
Registered: December 2011
Karma: 0
Senior Member
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
Re: classes in PHP [message #180387 is a reply to message #180386] Thu, 07 February 2013 11:17 Go to previous messageGo to next message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
Senior Member
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
Re: classes in PHP [message #180388 is a reply to message #180386] Thu, 07 February 2013 11:35 Go to previous messageGo to next message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
Senior Member
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
Re: classes in PHP [message #180389 is a reply to message #180386] Thu, 07 February 2013 11:46 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
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.
Re: classes in PHP [message #180390 is a reply to message #180389] Thu, 07 February 2013 13:46 Go to previous message
M. Strobel is currently offline  M. Strobel
Messages: 386
Registered: December 2011
Karma: 0
Senior Member
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.
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: change a string where have number after a word
Next Topic: Problem with readdir and ssh2
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Fri Sep 20 07:33:30 GMT 2024

Total time taken to generate the page: 0.03286 seconds