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
Return to the default flat view Create a new topic Submit Reply
Re: classes in PHP [message #180377 is a reply to message #180275] Wed, 06 February 2013 12:29 Go to previous messageGo to previous message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma:
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
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
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: Tue Nov 26 07:25:17 GMT 2024

Total time taken to generate the page: 0.04216 seconds