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


Groups > comp.lang.php > #8311

Re: classes in PHP

Message-ID <2693767.JdZqatdWKk@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2013-02-06 13:29 +0100
Subject Re: classes in PHP
Newsgroups comp.lang.php
References <fd3565a9-3766-4224-bed5-812a0cd11d64@googlegroups.com> <c42bd135-6e8f-46e3-be37-445b423ff529@googlegroups.com> <an7p7gF2ktvU1@mid.uni-berlin.de>

Show all headers | View raw


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

Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web